Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer Issue

 
Swati Singhal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a String in which there are 5 tokens taht are delimited by tabs ("\t"). I was using the StringTokenizer to parse this line and get individual tokens.
It worked fine, till I encountered a situation where there were 2 tabs (instead of 1).
Now, since StringTokenizer doesn't return me an empty string, how can I find out if there is a consecutive occurrence of that token. I know that the 3rd constructor of String Tokenizer has a boolean argument that can be set true, but it doesn't seem to be doing good to me!!

Please help ASAP!!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved from SCJP.

"Swats" while you are waiting for a response to your problem, please review our JavaRanch Naming Policy, and change your displayed name to conform with it.

Thanks
[ January 18, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of the legacy StringTokenizer (see API) why not use String.split?

Example:
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Swats",
I'm going to take a stronger stance than Barry.

Looking back through your history, it appears that you have changed your display name and it is now invalid. We rarely allow accounts with invalid display names to survive past 20 posts. Please change your display name immediately, or your account may be deleted without warning.

Dave.
 
Swati Singhal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,

Even split() doesn't give me an empty string"" or a null if there are two tabs in succession. What I want is, that if there are 2 consecutive tabs, then it should be able to detect that and give me an empty String or null which occurs between the two consecutive tabs.
 
Swati Singhal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,

It does work.
Thanks for the reply.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an optional boolean parameter on StringTokenizer that will give you the delmiters as well as the tokens. With that turned on you might get

The logic to walk through these is a bit tougher than the split technique, but it might be useful if you're stuck on an older JVM one day.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic