permaculture playing cards
The moose likes Beginning Java and the fly likes string tokenizer prob Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string tokenizer prob" Watch "string tokenizer prob" New topic

string tokenizer prob

macca cronin

Joined: Aug 05, 2004
Posts: 7

I'm not entirely sure how to use string tokenizers. i've got a string that is delimited with commas i.e.
StringTokenizer st1 = new StringTokenizer(a,",");

What i need to do now is store each of the tokens of the string so that I can use them later in my program. I'm trying something along the lines of
while (st1.hasMoreTokens()) {
String term = st1.nextToken();
System.out.println("Output: "+ term);
With this though, all I get is the tokens outputted. As i said I need to be able to call on them later so I need some way of storing them. Could anyone give me a clue to solving this problem as I'm kinda lost at the mo!

Thanks a million
Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102

while (st1.hasMoreTokens()) {
String term = st1.nextToken();
System.out.println("Output: "+ term); // change this line

Have a List, like

and then instead (or alongwith) of outputting them, add to the list

Now, you have the tokens saved in the list object, you can use them any way you want.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11817

You could do a couple of things... but first... do you really need to store them now, and use them later? could you use the tokenizer later, to get them as needed? (i really don't know - it depends on your specific needs. but it's something to think about).

assuming you really do need to pick them off now and store them, you could put them into one of the collections. a vector might work, or an arrayList, as might a few others. it would all depend on how you needed to get them back later and how you were going to use them...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

... and since sun is discouraging the usage of StringTokenizer, you may do:

and iterate trough aList as often as you like...
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Stefan's suggestion is a little simplistic as String.split() returns a String[]. I think it's a better option, but as far as I can see you'd have to do it like this:


[JK: Removed comma from s.split(",")]
[ August 18, 2004: Message edited by: Julian Kennedy ]
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
Try keeping it simple. This might appear lengthy but hey, it works.

Once u've declared the StringTokenizer object, create an array of strings. Declare this array as global, but initialize it in your method (so that you can use it in other methods, which seems to be your case). now using st1.countTokens(), initialize this array for the no of tokens, and now use a for loop to set values in each of the string array.

Got it???
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Julian is right, it returns a String[], but why not use that?

Julian removed the comma - I don't know why. Ah! I see, to match HIS example.

Shashank falls back to StringTokenizer - why?
And suggests: 'declare as global'.
How can you do that?

If you could, you shouldn't.

all we need is:

Since we don't know much more of the context, we cannot suggest for the code-design.
[ August 18, 2004: Message edited by: Stefan Wagner ]
I agree. Here's the link:
subject: string tokenizer prob
It's not a secret anymore!