Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

String Tokenizer Question

I know String Tokenizer is not the best way to solve this problem but i have to use it. Basically how do i setup the tokenizer to Extract all letters seperately.

So given the string "xyz|[" i need to get the tokens:
"x","y","z","|","[". Just wondering how to do this, and if i even can do this by just setting up a certain delimeter. Thanks.
[357 byte] By [p1kn1c] at [2007-11-11 10:21:39]
# 1 Re: String Tokenizer Question
I think the best approach is using String.toCharArray() and u will get a Character array. or use charAt() instead of StringTokenizer.
I don't understand why you are compelled to use StringTokenizer.
sudheerprem at 2007-11-11 22:31:40 >
# 2 Re: String Tokenizer Question
yeah its just for an assignment, but im using charAt now, will ask prof why he tells us to use Tokenizer, thanks.
p1kn1c at 2007-11-11 22:32:34 >
# 3 Re: String Tokenizer Question
I don't understand it, either. There is no "delimiter" to tokenize the string ... not even white space. How can you "split" the string ... I think sudheerprem's and your approach with charAt is the best way to address this problem.
nspils at 2007-11-11 22:33:32 >
# 4 Re: String Tokenizer Question
I had to do something similar and I used the split method.

I had to read all the lines from a file and count the number of occurrences of each letter.

while((textLine = MyFile.readLine()) !=null)

{

//Create an array to hold each of the words

//Remove any whitespace character (tab, new line, form feed,

//end of line, carriage return)

String[] numWords = newLine.split("\\s");

Hopefullt this will be of use to you as I found it a nightmare to do
AdRock at 2007-11-11 22:34:38 >
# 5 Re: String Tokenizer Question
AdRock: why wouldn't charAt do the same thing? Just iterate up the string, take charAt.

The problem which p1kn1c is dealing with is the assignment calls for him to use StringTokenizer.
nspils at 2007-11-11 22:35:36 >