aspose file tools*
The moose likes Beginning Java and the fly likes retrieve the particular element --  StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "retrieve the particular element --  StringTokenizer" Watch "retrieve the particular element --  StringTokenizer" New topic
Author

retrieve the particular element -- StringTokenizer

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
StringTokenizer tokens = new StringTokenizer( hai[i], ",\n");

here hai is the String array. How can i retieve the elements from the particluar position?
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
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 !!!


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
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

Joined: Apr 08, 2004
Posts: 1372
Any update?
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
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.
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
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

Joined: Apr 08, 2004
Posts: 1372
Mine is jdk 1.3. split() is supported from jdk 1.4 onwards...
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: retrieve the particular element -- StringTokenizer