Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting a memory location to a String

    Hi!

    I'm a computing student and am currently having problems with something that seems pretty simple to solve:

    I have a hotel room booking system whereby there are many Floor objects in an ArrayList container belonging to a Hotel, and in each Floor object there are many rooms again in an ArrayList.

    Hotel > Floor > Room

    (Room is an abstract class, specializations are BedRoom, StudyRoom, ConferenceRoom).

    Basically, to add a new Room to one of my previously created Floor objects I use:

    Code:
        String roomNumber = r.getRoomNumber();
        if(roomNumber == aRoomNumber) {
        // room number has already been taken
        }
        else {
        // go ahead and create the room
        }
    ...where "r" is my Room object, and getRoomNumber() simply returns the attribute theRoomNumber of that object.

    HOWEVER, with the above code, roomNumber equals something like 29a and aRoomNumber equals the user's input. The former is the MEMORY HEX LOCATION of the data that I would need in a string!

    So when the comparison is made, roomNumber (value 29a) and aRoomNumber (value 2) are NOT equal. Therefore my system is allowing a room number to be allocated to more than one room object.

    Solution? Thought of the toString method, but this is already being used to return ALL of a specified object's attributes.

    What else should I try? I could create something like another toString method, maybe toStringInput, whereby that would have a parameter of a string and i would just return that as a normal string? I think? Is that done? Is it possible/professional?

    This is driving me nuts, so any and all help is grealty appriciated!

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'll assume you are talking about Java code (since you didn't say what language). If this is the case you need to use the String.equals() method to compare String objects:
    Code:
    if(roomNumber.equals(aRoomNumber)) {
    shmoove

  • #3
    Registered User
    Join Date
    Jun 2002
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    oh i'm kicking myself that i didn't think of that! I've spent all day yesterday and the better part of today trying to find out why this was happening and how to fix it...

    Thank you, shmoove!

    (btw, your assumption = correct, java is the language of choice for this system).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •