• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nulls in StringTokenizer

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic