Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String Tokenizer  RSS feed

 
Gobiraj Vadivelrajan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
------------------
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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".
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!