Java How to Convert Token to String?

Donal.Lynch.Msc picture Donal.Lynch.Msc · Mar 7, 2011 · Viewed 15.3k times · Source

Cant seem to figure out how to convert a Token (StringTokenizer) into a String.

I have a String[] of keywords, and I read in multiple lines of text from a text file. StringTokenizer is used to chop the sentence up,,, at which point I need to pass each Token (the String representation of the Token) to a function for word analysis.

Is there a simple way to extract the string value from the token?

Any help appreciated guys....

Great help guys thanks again!!

Answer

Shawn picture Shawn · Mar 7, 2011

Use the method nextToken() to return the token as a string

StringTokenizer st = new StringTokenizer("this is a test");
         while (st.hasMoreTokens()) {
             wordAnalysis(st.nextToken());//sends string to your function
         }