aspose file tools
The moose likes Java in General and the fly likes StringTokenizer does not work properly. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "StringTokenizer does not work properly." Watch "StringTokenizer does not work properly." New topic
Author

StringTokenizer does not work properly.

Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 203

I am having problem with code...


String str = "24151*-*RS-EZT";
StringTokenizer Tok = new StringTokenizer(str, "*-*");
int n = 0;
while (Tok.hasMoreElements()) {
System.out.println("strToken:"+Tok.nextToken());
}


The output I am getting is:
strToken:24151
strToken:RS
strToken:EZT

but I am not expecting this output...I am expecting:::
strToken:24151
strToken:RS-EZT

Please let me know where I am wrong...

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

It's because the tokens "*" and "-" are used to delimit the strings. Use String#split instead (and be careful that "*" has a special meaning in regular expressions).


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer does not work properly.
 
Similar Threads
Tokenizing string in Java
how to print in capital of each frist word
parsing a string an basis of a delimetre
Regarding string tokenizer
retrieving value from a text box