File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringTokenizer" Watch "StringTokenizer" New topic
Author

StringTokenizer

Tyler Jordan
Ranch Hand

Joined: Nov 17, 2003
Posts: 70
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

Joined: Dec 19, 2001
Posts: 169
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

Joined: Feb 08, 2004
Posts: 1281
i wrote this, and runs fine:

prints:
C:\javas>javac Simbolo.java

C:\javas>java Simbolo
test
test
test
test
test
test


java amateur
Mark Wuest
Ranch Hand

Joined: Jun 07, 2003
Posts: 88
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

Joined: Feb 08, 2004
Posts: 1281
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
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

Joined: Feb 08, 2004
Posts: 1281
t1
t2
t3
Tyler Jordan
Ranch Hand

Joined: Nov 17, 2003
Posts: 70
Ok, I just needed to do nextToken() that fixed everything. Thanks
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
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

Joined: Feb 08, 2004
Posts: 1281
good point!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer