Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer and the "" empty string

 
jite eghagha
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

My strings look like this:
"001668","RTOFENTRY","LICENSE","","SJC","","00000431","Rental Car"
Notice the "" empty string.

My StringTokenizer's constructor takes care of delimiter characters
StringTokenizer token = new StringTokenizer(str, ",\"");

The problem is that the "" empty string is not counted as a token!

So instead of my token count to be 8, i get a 6.

How would you deal with this. Any suggestions besides me using a StringTokenizer.


Thanks for your time
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want the output to look like? What I'm asking is do you want the quotes included in the output, do you want just the characters between the quotes? Do you want to retain the commas?
 
jite eghagha
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"001668","RTOFENTRY","LICENSE","","SJC","","00000431","Rental Car"

I'd like my output to look like this:

001668 RTOFENTRY LICENSE "" SJC "" 00000431 Rental Car

This would be a row in a JTable, so the "" would actually just be an empty string or perhaps a null.

No commas or quotes in the out put.
[ August 08, 2008: Message edited by: jite eghagha ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably the most straightforward way to do it. It assumes that the input Strings will always be surrounded by "". If that's not the case, yuo may want to look into a more robust solution using the java.util.Pattern and java.util.Matcher regex classes.

 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I tried...Same as your code just have only token .... I m getting tokencount as 8 ....also note that relaceAll for qoute(")....


Hope this helps....
 
jite eghagha
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot, both solutions worked out.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use the org.apache.commons.lang.text.StrTokenizer class from the Apache Commons Lang Project.

This class has some helper metheds, e.g. the method 'setIgnoreEmptyTokens(boolean)'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic