• Post Reply Bookmark Topic Watch Topic
  • New Topic

empty tokens in String Tokenizer  RSS feed

 
Ram prakash
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am reading a String with "," as delimiter.
I use StringTokenizer to read values from String.
But there is a chance that I get no values for some tokens .
eg: 111,222,,444,,666
I have a problem in reading empty tokens.
StringTokenizer class is not considering empty tokens.
I get output as 111 222 444 666
Is there anyway I get that null values?
Please advice me.
Thanks,
Ram
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using 1.4, look at String.split(). I think it gives you a blank space for empty tokens, but I can't remember for sure. Look at the api to be sure.
Chad
 
John M Brown
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran into a similar problem using a pipe-delimeted record format using jdk1.3. The only way I was able to resolve this issue was to construct a StringTokenizer where the delimeters are also counted as tokens, and adding logic to find adjacent tokens (which have no value between them), and also for the 1st and last valuebeing null (in which the 1st or last charater of the string is also a delimeter)
Here is the code I came up with....
P.S. The code actually creates a hashmap with the key being the Integer position of the token, and the value being the token.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assorted past discussions of this topic can be found here, here, and here.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!