This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes StringTokenizer cannot split String with tabs? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer cannot split String with tabs?" Watch "StringTokenizer cannot split String with tabs?" New topic
Author

StringTokenizer cannot split String with tabs?

Mingwei Jiang
Ranch Hand

Joined: Feb 19, 2004
Posts: 63
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?




Thanks
Sameer Damle
Greenhorn

Joined: Jan 05, 2006
Posts: 14
just try this

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.


I think like I'm diagonally ||||<br />parked in a parallel universe.\\\\
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Is there a reason why people skip over String.split() in favor of the deprecated StringTokenizer? split() is much easier and seems to be very fast.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
From the API docs:


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();

_M_


Mike Noel
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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?




Thanks


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
[code]
String line = "\t\tsomething"
[code]
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.

Layne


Java API Documentation
The Java Tutorial
Mingwei Jiang
Ranch Hand

Joined: Feb 19, 2004
Posts: 63
Thank you, guys. I found the String.split API, and it's quite cool. It can accept regular expression.
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
On the lines of split. Is there an opposite? Like join? In php there is explode and implode. Of course you can do the implode in Java through a loop, but is there something like String.join(String [], delimiter)?? Yes I did check the String API. There is no String.join per see, but maybe someone knows a particular API that does include this.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I don't know of this anywhere in the API, but that doesn't mean much. You should be able to write a method that does this using StringBuilder fairly easily.

Layne
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I don't know of a "join", but for something quick and dirty I writeThe string is formatted: "[like, this, with, commas, and, square, backets]"


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer cannot split String with tabs?
 
Similar Threads
Reading a tab delimited txt file and replacing blanks with zeores.
StringTokenizer
Does File Uploading in Struts ignore Tab characters
StringTokenizer
How to read tab delimited file