StringTokenizer
StringTokenizer Count Tokens
This is an example of how to use a StringTokenizer to count the tokens of a String. The StringTokenizer is used to break a String into tokens. Using a StringTokenizer to count the tokens of a String implies that you should:
- Get a new StringTokenizer for a specified String, using the
StringTokenizer(String str)
constructor. - Invoke
countTokens()
API method of StringTokenizer. The method calculates the number of times that this tokenizer’snextToken()
method can be called before it generates an exception, that is the number of tokens that the String of the tokenizer has. - While
hasMoreTokens()
API method of StringTokenizer returns true, invokenextToken()
method of StringTokenizer to get the tokens of this String and invokecountTokens()
method again. Each time a new token is returned, thecountTokens()
method returns one less than before.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; import java.util.StringTokenizer; public class StringTokenizerCountTokens { public static void main(String[] args) { StringTokenizer tokenizer = new StringTokenizer("Java Code Geeks - Java Examples"); System.out.println("Remaining Tokens: " + tokenizer.countTokens()); // loop through tokens while (tokenizer.hasMoreTokens()) { System.out.println("Token:" + tokenizer.nextToken()); System.out.println("Remaining Tokens: " + tokenizer.countTokens()); } } }
Output:
Remaining Tokens: 6
Token:Java
Remaining Tokens: 5
Token:Code
Remaining Tokens: 4
Token:Geeks
Remaining Tokens: 3
Token:-
Remaining Tokens: 2
Token:Java
Remaining Tokens: 1
Token:Examples
Remaining Tokens: 0
This was an example of how to use a StringTokenizer to count the tokens of a String in Java.