• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

retrieve the particular element -- StringTokenizer

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringTokenizer tokens = new StringTokenizer( hai[i], ",\n");

here hai is the String array. How can i retieve the elements from the particluar position?
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just have a look at the following code.

import java.util.StringTokenizer;
public class strToken
{
public static void main(String args[])
{
String array[]= {"Hello How Are You", "How do u do"};
int i=0;
StringTokenizer strToken=new StringTokenizer(array[0]," ");
while (strToken.hasMoreElements())
{
System.out.println(strToken.nextElement());
}
}
}

In the above code we split the elements using space. The elemnets are stored in strToken. using nextElement Method we can retrieve the current element from strToken.
The Method hasMoreElements() reterive boolean value, whether the next element is available or not. I hope this will help u. If u have any other doubts just reply me.

Regards
Raghu J

cheers !!!
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i retive the value of particular token (If i have 1000 tokens, i want to retieve the value of 400th token)??
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any update?
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using StringTokenizer, you would have call nextToken() n times, where n is the token you require.

Alternatively, you could use String.split() to tokenize the string into a string array, then get the element you require from the string array by index number.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kri shan:
Any update?



possible in jdk 1.4.2 version.

String[] result = "this is a test".split("\\s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);

for 400th element, use result[399].also make sure you have a biggg String to tokenize.
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mine is jdk 1.3. split() is supported from jdk 1.4 onwards...
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kri shan:
Mine is jdk 1.3. split() is supported from jdk 1.4 onwards...



Please take the time to read the answers. People have told you how to do this with string tokenizer: write a loop calling nextToken() until you get to the token you require.
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic