The answer
The returned tokens are actually Strings, so you can do the usual stuff with them. I don't know what your exact requirements are but something like this should work:
List<String> operators = Arrays.asList("+", "-", "*", "/");
StringTokenizer token = ...;
while(token.hasMoreTokens()){
String next = token.nextElement();
if(operators.contains(next)){
//operator
}
else if(next.matches("\\d+")){
//number
}
else {
//error?
}
}
First of all you should use a use a variable for the next token, nextElement() also deletes the token from the Tokenizer so you have to keep it somewhere. Then we can determine the type by comparing String, here I check if it's part of a list of predetermined operators and whether it matches the regex \d+
, ie. whether it is a number.
Deprecation
The StringTokenizer JavaDoc says it has been deprecated in favor of String.split:
StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended
that anyone seeking this functionality use the split method of String
or the java.util.regex package instead.
So I would recommend against using this class.