• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string tokenizer prob

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic