aspose file tools
The moose likes Java in General and the fly likes StringTokenizer - why does it take [|] and [] as the same? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "StringTokenizer - why does it take [|] and [] as the same?" Watch "StringTokenizer - why does it take [|] and [] as the same?" New topic
Author

StringTokenizer - why does it take [|] and [] as the same?

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
I'm writing to code to tokenise this string

String theString = "123@star.com[|]lsep@mail.com,00887217904202[&]mmey@mail.com,00884457850456[&]00882131420000@star.com,[|]False[|][|]31/05/2004 08:48:43[|][|]"

StringTokenizer st = new StringTokenizer(theString, "[|]");

When I read it I get; (I'm counting the tokens at the start of the line)
8 123@satstar.com
7 lsep@mail.com,00887217904202
6 &
5 mmey@mail.com,00884457850456
4 &
3 00882131420000@satstar.com,
2 False
1 31/05/2004 08:48:43

The question is, why do the &'s next to 6 and 4 appear? And I am only expecting 6 tokens. Can anyone give some advice?

Thanks, Rachel
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Hi,

the "problem" is that the constructor does not work as you assume.
StringTokenizer st = new StringTokenizer(theString, "[|]");

The tokenizer does not use "[|]" as a delimiter, but each occurrence of any of the characters in the string, "[" or "|" or "]"

Bart
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Well, I guess that makes sense...

Thanks!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: StringTokenizer - why does it take [|] and [] as the same?
 
Similar Threads
Portal Server v6.0 Initialization Problem
endsWith()
Help me check my criteriaFind method
String.split Vs StringTokenizer
Need help with read method