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!开发者_JS百科!
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
}
nextToken()
called on a StringTokenizer, already returns a String
. You don't need to convert that String
to a String
.
A Token is just a small (string) portion of a larger String. Tokens are used in various forms of processing, and a word (the word token) is needed to differentiate the category of items from the specific items.
For example, every word, period, and comma in this string is a token.
Would lend itself to the following tokens (all of which are strings):
For
example
,
every
word
,
period
,
and
comma
in
this
string
is
a
token
.
for(int i = 0; i<stringArray.length; i++) {
StringTokenizer st = new StringTokenizer(stringArray[i]);
while(st.hasMoreTokens()) {
callMyProcessingMethod(st.nextToken());
}
}
StringTokenizer is now a legacy class in Java. As of Java 1.4 it is recommended to use the split() method on String which returns a String[].
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
精彩评论