• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer

 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic