• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

empty tokens in String Tokenizer  RSS feed

 
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
 
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
 
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.
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!