aspose file tools
The moose likes Java in General and the fly likes Nulls in StringTokenizer 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 "Nulls in StringTokenizer" Watch "Nulls in StringTokenizer" New topic
Author

Nulls in StringTokenizer

Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Hi ,

I have a tab separated file and I have to port the data to DB through StringTokenizer.Every thing is worrking fine, except whenever there is a "null", String Tokenizer simply ignores it.

For example,

If my string is : - TERMINOLOGY<TAB>GOOGLE (<TAB> signifies tab)
then I will get two tokens as TERMINOLOGY and GOOGLE.

But if I have TERMINOLOGY<TAB><TAB>GOOGLE
then I should get three tokens instead.
TERMINOLOGY,null(or space) and GOOGLE.
But StringTokenizer returns only two.TERMINOLOGY and GOOGLE.

I have an alternate way to do this through character matching then I will have to match each and every character in my TAB separated file, which will be too time taking.

Would be grateful if anyone helps me.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Try,



Basically StringTokenizer is old and rubbish, and still there soas not to break legacy code. You should use regular expressions instead, where possible.

You can write your own StringTokenizer using regular expressions, use one of the many free implementations out there, or if you are using 1.5, you could look at java.util.Scanner.

Hope this helps
[ March 09, 2005: Message edited by: Horatio Westock ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You could use the StringTokenizer constructor which also returns the delimiters as tokens:

Then you could cehck eack token and handle it if it is not a delimiter.

Alternatively, as the StringTokeniser JavaDocs suggest, you could use the split method of the String class, which will include the "null" value in the array it returns.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


But StringTokenizer returns only two.TERMINOLOGY and GOOGLE.


That's what the api promises and that's how it will work. You should write your own logic if you want it different. Probably String.split(String regex) method may help, check it out.

ram.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Nulls in StringTokenizer
 
Similar Threads
Alternative to StringTokenizer
Reading strings from file
Alternative to StringTokenizer
StringTokenizer cannot split String with tabs?
Reading a tab delimited txt file and replacing blanks with zeores.