Java includes various classes to apply multiple sorts of functionalities as per the given requirements. The “StringTokenizer” is one such class that comes into effect while working with the string values. This class can be effective while testing for multiple string records without getting an error, thereby streamlining the code functionalities at the programmer’s end.
This blog will demonstrate the working of the “StringTokenizer” class in Java.
How to Use “StringTokenizer” in Java?
The “StringTokenizer” class in Java has a tokenizer String method that splits a string into tokens with respect to a specified delimiter.
Syntax
In this syntax:
-
- “string” refers to the “String” that needs to be evaluated.
- “de” corresponds to a delimiter. It is such that if the “return” value is true, the delimiter characters are considered to be tokens. Else, these characters act as separate tokens.
“StringTokenizer” Methods
Following are the “StringTokenizer” class methods along with their usage:
Methods | Usage |
hasMoreTokens() | It applies a check for more tokens available. |
nextToken()
|
It gives the next token from the “StringTokenizer” object. |
nextToken(String delim) | It gives the next token with respect to the specified delimiter. |
hasMoreElements() | It applies the same functionality as the “hasMoreTokens()” method. |
nextElement() | It is identical to the “nextToken()” method but its return type corresponds to an “Object”. |
countTokens() | It computes the total tokens. |
Before heading to the examples, import the following package to work with the “StringTokenizer” class:
Example 1: Utilizing the “StringTokenizer” Class in Java
In this example, the “StringTokenizer” class can be utilized and the contained string(as a constructor parameter) can be displayed:
public static void main(String args[]){
StringTokenizer string = new StringTokenizer("Java"," ");
System.out.println(string.nextToken());
}}
In this code snippet:
-
- First of all, create a “StringTokenizer” object with the help of the “new” keyword and the “StringTokenizer()” constructor.
- Also, pass the stated string as a constructor parameter that needs to be displayed.
- Lastly, apply the “nextToken()” method to access and return the specified string(as a constructor parameter).
Output
As analyzed, the string value of the parameterized constructor is returned appropriately.
Example 2: Applying the “StringTokenizer” Class Methods in Java
This particular example implements the “StringTokenizer” class methods upon the two different strings:
public static void main(String args[]){
StringTokenizer string1 = new StringTokenizer("Java,Programming");
StringTokenizer string2 = new StringTokenizer("Linux hint");
System.out.println("Total Tokens in string1 -> "+string1.countTokens());
System.out.println("Total Tokens in string2: "+string2.countTokens());
while (string1.hasMoreTokens()) {
System.out.println(string1.nextToken(","));
System.out.println(string1.nextToken(","));
while (string2.hasMoreElements()) {
System.out.println(string2.nextElement());
System.out.println(string2.nextElement());
}}}}
Apply the below-provided steps in accordance with the above code lines:
-
- First, similarly create a “StringTokenizer” class object having the stated string separated by a comma “,”.
- Likewise, accumulate another string in the latter created object.
- Now, associate the StringTokenizer “countTokens()” method with both the objects to return the count of tokens in both the contained strings.
- After that, apply the “hasMoreTokens()” method with the former object to check for available tokens and return them based on the check via the “nextToken()” method.
- It is such that each of the next string values is separated by a comma.
- Likewise, apply the “hasMoreElements()” method with the latter string to analyze if there is another string element and display it based on that using the “nextElement()” method.
Output
This outcome implies that both the objects comprising the string values are coped in accordance with the applied methods.
Conclusion
The “StringTokenizer” class in Java gives a tokenizer String method to split a string into tokens with respect to the specified delimiter and return the string based on its methods. This class is assistive in working with the strings as it can cope with the faced exceptions as well via its methods. This blog demonstrated the objective and working of the “StringTokenizer” class in Java.