aspose 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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringTokenizer" Watch "StringTokenizer" New topic
Author

StringTokenizer

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Dear all,
I have code like this
public class Test
{
public static void main(String[] args)
{
String temp = "07.00.00.00";
StringTokenizer st = new StringTokenizer(temp, ".00");
String data = st.nextToken();
System.out.println("data = " + data);
}
}
The output of this is 7.
Why is this? Because i think this will output 07 instead of 7....
daniel
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Fisher,
I'm afraid StringTokenizer is never going to return "07" as a single token with the String "07.00.00.00" and a delimiter String of ".00" (which is redundant since ".0" would accomplish the same thing). The constructor you used causes the tokenizer to discard all delimiters. So you only have one token: "7". Now you can use a constructor that returns delimiters as tokens:

But that will return "0" for the first token, "7" for the second, "." for the third, etc.
If you change the delimiter to "." then you will get back the tokens "07", "00", "00", and "00".
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If what you want is to consider the string ".00" as a single delimiter, try

Note that '.' is a special character as far as regular expressions are concerned (see API for the split() method), so you need \\ to escape it.
I don't know if that's what you were looking for, but it's worth a try...


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer