Reputation: 3357
I have pipe delimited strings containing id, latitude, longitude, refname, userid, username:
1390|-32.5506782531738|18.9797592163086|Hardie - Vrede 4|21|Hardie van der Merwe
1391|-33.5143165588379|18.422327041626|test|3|sgroenewald
1392||||3|sgroenewald
When I use a StringTokenizer class to read each of these lines, they work, until I hit line 3, where there are blank values. I read the ID ok, then I check hasMoreTokens(), which returns true, but it skips over the blanks and goes to the second last field, which is read as 3 using nextToken, but into the wrong variable in my code.
Continuing on I read the next field using nextToken, expecting it to be longitude, but I get a string back (sgroenewald) and the wheels come off..
Is there a way to see if the next token is blank, before reading it?
Upvotes: 1
Views: 633
Reputation: 7256
Try to use constructor of StringTokenizer with 3 params, specifying returnDelimiters
as true
. Of course you need to manually remove delimiters from values itself while cycling tokenizer.
Upvotes: 1
Reputation: 29632
One thing you can do is, replaces these blank spaces with no space ( " " with "" ) using replace() method of String Class.
String finalStr = "1390|-32.5506782531738|18.9797592163086|Hardie - Vrede 4|21|Hardie van der Merwe";
finalStr = finalStr.replaceAll( " ", "" );
Upvotes: 0