• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

String Tokenizer

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to read a "|" delimited file. Each value/token represents a field
The data looks like
HDR|1000|009|1|99|1|20011210| ||WILSON|10X|HeSaab Document|1|USD|USA|6|2001|12| |143.00| |WILSON|WILSON| ||
If I use the code below the nextToken skips if the delimiters
are continuous like | || then these values are skipped.
I want these values to be returned as empty or null when I invoke
nextToken.
Is there any workaround....
StringTokenizer tokenizer = new StringTokenizer(line,"|",false);
int i = 0;
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
Thanks
------------------
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this is the best way, but it should work ...
When you created your StringTokenizer, you specified that you did not want it to return delimiters as tokens. If, instead, you specify that you do want delimiters returned as tokens, then you can test for the special case of "this token is a delimiter and so was the last one".
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic