This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string tokenizer prob

 
macca cronin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Jules

[JK: Removed comma from s.split(",")]
[ August 18, 2004: Message edited by: Julian Kennedy ]
 
Shashank Agarwal
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
Uh!
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic