Hi, I have been trying to get this simple function to work as it is stated in the reference library. Instead of changing from true to false after going through the characters and breaking the loop, it keeps resolving as true indefinately. It doesn't break out of the while loop. Have no idea what I am doing wrong.
while (parser.hasMoreTokens()) { System.out.println(parser.nextToken()); numToken++; }
I hope just giving you the "answer" didn't circumvent you from learning how it works. There's a little sample snippet in the javadocs for StringTokenizer along with an advertisement for the much-easier-to-use (IMHO) String.split() method if you're using 1.4 or later.
Mark
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
i ignore the meaning of Wheaties but i guess what you mean is that, if we dont iterate: parser.nextToken(); then the conditional remains true forever
Mark Wuest
Ranch Hand
Joined: Jun 07, 2003
Posts: 88
posted
0
Yes. Unless there are *no* tokens at all, in which case hasNext() would always be false.
Also, did you check out the java.lang.String.split() method? It gives you back a String[] that I find easier to work with: you can either iterate through with a for(; ;) or you can directly access any element. Again, notice that split() only works in 1.4 and later versions.
Wheaties is a breakfast cereal in the U.S. that used to be advertized as "The Breakfast of Champions." I haven't seen a commercial for it in years, though.
Mark [ May 20, 2005: Message edited by: Mark Wuest ]
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
t1 t2 t3
Tyler Jordan
Ranch Hand
Joined: Nov 17, 2003
Posts: 70
posted
0
Ok, I just needed to do nextToken() that fixed everything. Thanks
If I am not mistaken, it seems as though you are counting the number of tokens in your query word. The StringTokenizer class already has a method called countTokens() which does the same thing.