File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieve the particular element -- StringTokenizer

 
kri shan
Ranch Hand
Posts: 1453
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Raghu Shree
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1453
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1453
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any update?
 
Horatio Westock
Ranch Hand
Posts: 221
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1453
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mine is jdk 1.3. split() is supported from jdk 1.4 onwards...
 
Horatio Westock
Ranch Hand
Posts: 221
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic