Skip to content

Java StringTokenizer – Get Array of Tokens

Java StringTokenizer get an array of tokens example shows how to get string tokens in a String array from the StringTokenizer object.

How to get an array of string tokens from the StringTokenizer?

The StringTokenizer class can tokenize the string data into tokens using the delimiters we provide. Once these tokens are generated from the string, we can iterate through the tokens using the nextToken or nextElement method.

These methods return the next token from the string from the current position. There are also methods, namely hasMoreTokens and hasMoreElements, that returns true if there are more tokens in the string content.

However, the StringTokenizer class does not have any method that returns tokens in the form of a String array. If we want, we need to do that manually by iterating the tokens using the provided methods.

In the code given below, I have created a method that iterates through the string tokens using hasMoreTokens and nextToken methods. It then adds each token element to the String array.

Output

I have used the countTokens method of the StringTokenizer class to get the total number of tokens available in the string. We need to create the String array that can fit that many elements.

Important Note:

Except for educational purposes, it is suggested to use the Java String split method instead of the StringTokenizer class to generate string tokens. The StringTokenizer class is there just for backward compatibility purposes.

If you want to learn more about the tokenizer, please visit the Java StringTokenizer tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.