Hi all,
I need to break up a large string into individual words, and then compare the
first word of the large string with a different word.
I can get the first word of the large string and display it and I can also
display the word I want to compare it with, but when I compare them, it says
they are different, even though they aren't! Here is my code:
public void listAppointments(int dayNo)
{
...
String dayNumber = "" + dayNo;
String stToken;
for (int index = 0; index < daySlots.size(); index++) {
daySlots.get(index);
StringTokenizer st = new StringTokenizer(theAppointment.toString()
);
stToken = "" + st.nextToken();
if (stToken == dayNumber) {
System.out.println(theAppointment.toString());
}
else {
System.out.println("Fail");
}
}
}
stToken takes the first word from the large string and turns it into an
individual String, dayNumber is the string I want to compare it with.
Even though these are both the same when I output them, "Fail" is still
outputted when I compare them. Can anyone tell me why this is and advise me
on a solution? Please don't give me the code for it as this is coursework!!!
Thomas Hawtin - 22 Aug 2005 18:27 GMT
> String dayNumber = "" + dayNo;
> String stToken;
> [...]
> if (stToken == dayNumber) {
They are different object with equal values. Use
token.equals(dayNumber). == on reference types is quite obscure.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Eric Sosman - 22 Aug 2005 18:43 GMT
> Hi all,
> I need to break up a large string into individual words, and then compare the
[quoted text clipped - 5 lines]
> [...]
> if (stToken == dayNumber) {
if (stToken.equals(dayNumber)) {
The two variables are references to String objects.
Your `==' operator tests whether they happen to refer to
the same String object, while the equals() method asks
whether the two String objects have identical content.
Write your name on two pieces of paper. Put one of
them (stToken) in your left pocket and the other (dayNumber)
in your right. Does your left pocket contain the same
piece of paper as your right pocket? No, of course not:
you have two distinct pieces of paper, each with its own
independent identity. Do these two pieces of paper show
the same name? Yes: the pieces of paper are distinct, but
you wrote the same thing on both of them.

Signature
Eric.Sosman@sun.com
makzan - 22 Aug 2005 19:41 GMT
Thankyou, I should have known that. Pretty dissappointed in myself!!
You couldn't tell me how to do a tab in a string could you?
Such as
System.out.println("Time: (***TAB SPACING***) 0900");????
Eric Sosman - 22 Aug 2005 19:49 GMT
> Thankyou, I should have known that. Pretty dissappointed in myself!!
> You couldn't tell me how to do a tab in a string could you?
> Such as
> System.out.println("Time: (***TAB SPACING***) 0900");????
System.out.println("Time:\t0900");
System.out.println("I think you need a Java textbook.");

Signature
Eric.Sosman@sun.com
makzan - 22 Aug 2005 20:03 GMT
I do have a Java textbook, but it doesn't say how to put a tab in a string in
the book.
> System.out.println("I think you need a Java textbook.");
> System.out.println("I think you need some manners, as I am only a beginner, but thank you anyway. It is very kind of you to help and much appreciated.");
Eric Sosman - 22 Aug 2005 21:06 GMT
> I do have a Java textbook, but it doesn't say how to put a tab in a string in
> the book.
Either you haven't looked hard enough, or your textbook
is too advanced (that is, it assumes you already know core
Java and is teaching you something else).
>> System.out.println("I think you need a Java textbook.");
>
>> System.out.println("I think you need some manners, as I am only a beginner, but thank you anyway. It is very kind of you to help and much appreciated.");
makzan, I gave you two answers to two questions, and you
found both my answers helpful. I should have known it was
unmannerly to help you; I apologize for my rudeness and will
strive earnestly not to help you any more.

Signature
Eric.Sosman@sun.com
Monique Y. Mudama - 22 Aug 2005 19:56 GMT
> if (stToken.equals(dayNumber)) {
>
[quoted text clipped - 11 lines]
> of paper are distinct, but you wrote the same thing on both of
> them.
That being said, I could have sworn that it used to be guaranteed that
equivalent Strings would be stored in the same place. It worked up
through 1.4. When I ran my code on 1.5, though, it broke. Maybe it
wasn't guaranteed, but it was always (in my experience) the case.
Still, I don't know what possessed me to use == to compare Strings,
even if it was working. Which it now isn't.

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Thomas Hawtin - 22 Aug 2005 20:02 GMT
> That being said, I could have sworn that it used to be guaranteed that
> equivalent Strings would be stored in the same place. It worked up
> through 1.4. When I ran my code on 1.5, though, it broke. Maybe it
> wasn't guaranteed, but it was always (in my experience) the case.
String literals and Strings returned from String.intern are guaranteed
to be the same object iff they have the same value. Although early Sun
implementations (up to 1.1?) were buggy in this respect.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Monique Y. Mudama - 22 Aug 2005 20:18 GMT
>> That being said, I could have sworn that it used to be guaranteed
>> that equivalent Strings would be stored in the same place. It
[quoted text clipped - 6 lines]
> Although early Sun implementations (up to 1.1?) were buggy in this
> respect.
Well, maybe that's where I got the idea. But it is true that my code
was working in 1.4 comparing strings with ==; it broke in 1.5. Again,
my fault for being sloppy.

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Sean - 22 Aug 2005 21:41 GMT
On
>Hi all,
>I need to break up a large string into individual words, and then compare the
[quoted text clipped - 4 lines]
>
> public void listAppointments(int dayNo) //your should change the
parameter type to String
> {
>....
[quoted text clipped - 23 lines]
>outputted when I compare them. Can anyone tell me why this is and advise me
>on a solution? Please don't give me the code for it as this is coursework!!!
I made a few changes, but I think you should consider putting the individual
String tokens in an array:
StringTokenizer st = new StringTokenizer( theAppointment.toString() );
String[] words = new String[ st.countTokens() ];
int i = 0;
while ( st.hasMoreTokens() )
{
words[ i ] = st.nextToken();
i++;
}
Now you have an array of neatly organized words that you can choose from, enjoy!
Sean - 22 Aug 2005 21:43 GMT
On
>On
>>Hi all,
[quoted text clipped - 47 lines]
>
>Now you have an array of neatly organized words that you can choose from, enjoy!
Oops, I forgot something.