Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-21-2004, 10:54 AM   PM User | #1
sarah_anne
Registered User

 
Join Date: Jun 2002
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
sarah_anne is an unknown quantity at this point
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!
sarah_anne is offline   Reply With Quote
Old 12-21-2004, 12:34 PM   PM User | #2
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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
shmoove is offline   Reply With Quote
Old 12-21-2004, 04:33 PM   PM User | #3
sarah_anne
Registered User

 
Join Date: Jun 2002
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
sarah_anne is an unknown quantity at this point
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).
sarah_anne is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:34 AM.


Advertisement
Log in to turn off these ads.