File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer" Watch "StringTokenizer" New topic
Author

StringTokenizer

Shane Roylance
Ranch Hand

Joined: Aug 29, 2001
Posts: 72
Hi all,
Hopefully this is a really simple question for someone out there. I have a string that I am parsing that looks like this "some string<token><token>Another string<token>Last string<token>" or at times it could look like this. The place where two tokens are together is where I have a blank string. Using the nextToken() method of StringTokenizer, it seems to skip the blank string, so the second string I get from next token is "Another string" . Am I halucinating, or is this how it is supposed to work. It seems to me that if two tokens are next to each other it would return a blank string.
Anyone know how this is really supposed to work?
Thanks
Shane
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that StringTokenizer has a constructor that takes a boolean as the last argument. It might provide the function that you are after.
Otherwise, use of the java.util.regex package might be more appropriate to solve your problem.Displays:
some string
Another string
Last string
[ August 19, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[I swear, Dirk's code sample wasn't there when I first wrote this...]
StringTokenizer is pretty inconvenient for this sort of thing. That boolean argument Dirk mentions modifies its behavior, but it's still not very easy to use. There are various workarounds, but probably the easiest is to use the String split() method in JDK 1.4:

This has two big advantages: (1) it handles multi-character tokens the way most people would expect, and (2) if two tokens are next to one another it returns a blank string for that field.
[ August 19, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Shane Roylance
Ranch Hand

Joined: Aug 29, 2001
Posts: 72
Thank you both,
Two solutions that both work great.
Thanks again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: StringTokenizer