wood burning stoves 2.0
The moose likes Beginning Java and the fly likes String Tokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Tokenizer" Watch "String Tokenizer" New topic

String Tokenizer

sreeni san
Ranch Hand

Joined: Aug 14, 2001
Posts: 39
I need to tokenize a string with ',' as delimiter.
for example in the string below:
Sreeni, san, 12345, , #44 XYZ street,,234567
Here I need to tokenize with comma as delimiter.
The problem is with the value is not specified and instead when ,, is specified. The StringTokenizer skips this as a value, whereas , , is returned as blank.
How do I overcome this problem, when ,, is specified.
Note: comma character in the above string is not a value.
aminur rashid
Ranch Hand

Joined: Sep 21, 2001
Posts: 79
Hi sreeni.
I have tried to solve ur problem.You can have other logic for this also.What i have done in this function is that i have used a temproryTokenizer.From this temproryTokeniser i have eliminated the tokens with values" " and null
Then i have made the finalToknizer


Regards<br />Aminur
sreeni san
Ranch Hand

Joined: Aug 14, 2001
Posts: 39
Thanks for the reply.
The problem I have is that I need to retrieve token when the value is ,, . The StringTokenizer just skips this.
I'll try to modify my example:
I have data pertaining to a user for the fields below:
fName, lname, userId, address1, address2,city, phone
The corresponding values for the above fields are
Sreeni, san, 12345, , #44 XYZ street,,234567
Now I tokenize the entire String and assign values to respective variables. While doing so I have encountered problems like for example:
The value for fName, lname, userId, address1, address2 are returned correctly as tokens.
The problem here is with city as the value ",," is skipped and 234567 is returned as next token immediately after address2.
In case of address1 ", ," is value is returned as blank, which is fine for me.
Kindly suggest me a suitable solution.

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
What if you replace ,, with , , or ,"", or ,null, before you use the StringTokenizer?

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
aminur rashid
Ranch Hand

Joined: Sep 21, 2001
Posts: 79
Hello the problem with that is ''while ' ' makes a string with a space character.
You can try one thing .Use the constructer

It will return values the delimiter
, after every string.Write a method to create a null or "" string if u get two , without getting any values.Hope this will help
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
If you're using SDK 1.4, you can handle this easily using the String class' split() method. Otherwise, here's a class I wrote some time ago to solve this problem:

[ April 03, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
I wrote a stringtokenizer that can handle null tokens:
I also wrote a CSV parser that will even handle quoted values:
I agree. Here's the link: http://aspose.com/file-tools
subject: String Tokenizer
It's not a secret anymore!