Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer

 
Tyler Jordan
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jeff Grant
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm betting it is only going through the while (numLine <= linecount) loop once, but it's getting hung at the while (parser.hasMoreTokens()) instead.

You should look into why while (parser.hasMoreTokens()) is never going false.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote this, and runs fine:

prints:
C:\javas>javac Simbolo.java

C:\javas>java Simbolo
test
test
test
test
test
test
 
Mark Wuest
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's your loop:

while (parser.hasMoreTokens())
{
numToken++;
}

Here's your loop after it has had its Wheaties:

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
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Unless there are *no* tokens at all, in which case hasNext() would always be false.

Did you check out the javadoc at

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

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
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t1
t2
t3
 
Tyler Jordan
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I just needed to do nextToken() that fixed everything. Thanks
 
K Riaz
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Just so you know.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good point!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic