For Programmers: Free Programming Magazines  


Home > Archive > Java Help > August 2005 > Comparing a token and a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Comparing a token and a string
makzan via JavaKB.com

2005-08-22, 7:04 pm

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!!!


--
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.as...-setup/200508/1
Thomas Hawtin

2005-08-22, 7:04 pm

makzan via JavaKB.com wrote:
> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Eric Sosman

2005-08-22, 7:04 pm



makzan via JavaKB.com wrote:
> 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:
>
> [...]
> 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.

--
Eric.Sosman@sun.com

makzan via JavaKB.com

2005-08-22, 7:04 pm

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");????


--
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.as...-setup/200508/1
Eric Sosman

2005-08-22, 7:04 pm



makzan via JavaKB.com wrote:
> 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.");

--
Eric.Sosman@sun.com

Monique Y. Mudama

2005-08-22, 7:04 pm

On 2005-08-22, Eric Sosman penned:
>
>
> 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.


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.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
makzan via JavaKB.com

2005-08-22, 7:04 pm

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.");



--
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.as...-setup/200508/1
Thomas Hawtin

2005-08-22, 7:04 pm

Monique Y. Mudama wrote:
>
> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Monique Y. Mudama

2005-08-22, 7:04 pm

On 2005-08-22, Thomas Hawtin penned:
> Monique Y. Mudama wrote:
>
> 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.
>


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.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Eric Sosman

2005-08-22, 7:04 pm



makzan via JavaKB.com wrote:
> 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).
[color=darkred]
>
>

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.

--
Eric.Sosman@sun.com

Sean

2005-08-22, 7:04 pm

On
>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) //your should change the

parameter type to String
> {
>....
> 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!!!
>
>


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

2005-08-22, 7:04 pm

On
>On
>parameter type to String
>
>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!

Oops, I forgot something.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com