• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

StringTokenizer cannot split String with tabs?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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_
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, guys. I found the String.split API, and it's quite cool. It can accept regular expression.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]"
 
reply
    Bookmark Topic Watch Topic
  • New Topic