View Full Version : Converting a memory location to a String

12-21-2004, 11:54 AM

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:

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!

12-21-2004, 01:34 PM
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:

if(roomNumber.equals(aRoomNumber)) {


12-21-2004, 05:33 PM
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! :thumbsup:

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