StringTokenizer st = new StringTokenizer("your string containg tabs");
this should work.... and by the way just go through javadoc for StringTokenizer class you will get much more information.
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
I second the suggestion to use String.split();
Originally posted by Mingwei Jiang:
I read one line from the file that is separated by tab, and no matter I use "\t" or " " (tab in double quotes), it doesn't work. It can only give me one token instead of 12 which is correct.
Can anyone help me?
I don't see anything wrong with this code. Are you sure that line has tab characters in it? Also, are there any characters in between the tab characters. If for example, you have something like
String line = "\t\tsomething"
Then StringTokenizer will only return a single token. By default it will not return the "null token" between the two tab characters. I believe there is a method to force it to do so, however.
With that said, I agree that you should use String.split() or regular experessions instead, if you are using a version of Java that supports these.