I want to use StringTokenizer to convert a string like
dasbndc|716372|3|5|||||abcd
to an array:
array[0] = dasbndc
array[1] = 716372
array[2] = 3
array[3] = 5
array[4] = "" <============= empty string !!!
array[5] = "" <============= empty string !!!
array[6] = "" <============= empty string !!!
array[7] = "" <============= empty string !!!
array[8] = abcd
I initialized the tokenizer with
tk = new StringTokenizer(line,"|");
The following never returns the blank tokens:
while (tk.hasMoreTokens())
{
System.out.println(tk.nextToken());
}
Any thoughts/replies welcome
Vincent
Chris Smith - 15 Oct 2003 19:30 GMT
> I want to use StringTokenizer to convert a string like
> dasbndc|716372|3|5|||||abcd
[quoted text clipped - 8 lines]
> array[7] = "" <============= empty string !!!
> array[8] = abcd
StringTokenizer doesn't work like that. There are kludges, but ideally,
you'd use a class that does. For example, Jon's JlsTokenizer at
http://www.yoda.arachsys.com/java/skeetutil/.

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Paul Lutus - 15 Oct 2003 19:46 GMT
> I want to use StringTokenizer to convert a string like
> dasbndc|716372|3|5|||||abcd
[quoted text clipped - 8 lines]
> array[7] = "" <============= empty string !!!
> array[8] = abcd
Since StringTokenizxer is known not to be able to handle this problem, and
IMHO is really dreadful to use, why not do this instead:
String source = "dasbndc|716372|3|5|||||abcd|||";
String[] array = source.split("\\|",-1);
The "-1" argument forces processing of trailing blank fields. Note that I
included some in the data for this test.
Like this:
**************************************************
public class Test {
public static void main(String[] args)
{
String source = "dasbndc|716372|3|5|||||abcd|||";
String[] array = source.split("\\|",-1);
for(int i = 0;i < array.length;i++) {
System.out.println("[" + array[i] + "]");
}
}
}
**************************************************
Result:
[dasbndc]
[716372]
[3]
[5]
[]
[]
[]
[]
[abcd]
[]
[]
[]

Signature
Paul Lutus
http://www.arachnoid.com
TresWBL - 16 Oct 2003 18:58 GMT
> String source = "dasbndc|716372|3|5|||||abcd|||";
>
> String[] array = source.split("\\|",-1);
>
> The "-1" argument forces processing of trailing blank fields. Note
> that I included some in the data for this test.
I'm wondering, how did you know that the split method could take a second
argument? I looked here ( http://java.sun.com/j2se/1.4.2/docs/api/ ) and it
only showed split having one argument. Thanks.

Signature
TresWBL
telconstar99@NOSPAMhotmail.com
Paul Lutus - 16 Oct 2003 21:00 GMT
>> String source = "dasbndc|716372|3|5|||||abcd|||";
>>
[quoted text clipped - 6 lines]
> argument? I looked here ( http://java.sun.com/j2se/1.4.2/docs/api/ ) and
> it only showed split having one argument.
Yes, that's true, but only if you don't actually read it completely.
> Thanks.
Okay, all right. I confess. I cheated. I apologize. I ... I ... (gasp,
choke) ... read the documentation! (breaks down and weeps uncontrollably).
Here is an excerpt from the same document set you visited. Put it on one
line:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
#split(java.lang.String,%20int)

Signature
Paul Lutus
http://www.arachnoid.com
Tor Iver Wilhelmsen - 15 Oct 2003 20:34 GMT
> tk = new StringTokenizer(line,"|");
Try a third parameter to the constructor - check the docs.
> Any thoughts/replies welcome
You need to return the delimiters as tokens in order to have
StringTokenizer not skip multiple delimiters.
But if you can use 1.4.x APIs, do as others have suggested and use
String.split().
RC - 15 Oct 2003 21:01 GMT
> I want to use StringTokenizer to convert a string like
> dasbndc|716372|3|5|||||abcd
[quoted text clipped - 17 lines]
> System.out.println(tk.nextToken());
> }
String tmpstr = "";
while (tk.hasMoreTokens()) {
tmpstr = tk.nextToken();
if (tmpstr == null)
tmpstr = "";
System.out.println(tmpstr);
}
Much better
String[] array = line.split("|");
Stephen Ostermiller - 21 Oct 2003 23:53 GMT
Use the com.Ostermiller.util.StringTokenizer which has a method that
you can set to have it return empty tokens.
http://ostermiller.org/utils/StringTokenizer.html