StringTokenizer Class in java

Before going into the details of StringTokenizer class let us first understand the following two terms:
1) Token
2) Tokenizer


Token

The Sequence of consecutive characters without any delimiter is known as a Token.
In other words a token is also known as word (i. e. sequence of letters without space) .
For example :
Consider a sentence/string is "programming language is fun". This string consist of three(3) tokens or words i. e. "programming" , "is" and "fun".
The delimeter in the above string is ' ' (blank space).

Tokenizer

The process which create tokens is known as Tokenizer.
To implement token and tokenizer the StringTokenizer class is used.

StringTokenizer class

The class which splits text or string into tokens is known as StringTokenizer class.
A token is sequence of consecutive characters that are not delimiters (it can also be called a word).
The StringTokenizer class splits the text or string into tokens/words by taking delimiters into account. if we do not pass any delimiter than by default StringTokenizer uses ' ' space character as delimiter and splits the string according to space .

  A StringTokenizer is a utility which is available in the package " java.util ".

  StringTokenizer class can be used as :
"import java.util.StringTokenizer ;" or "import java.util.*;" .

  It can also we used by referring the fully qualified name of StringTokenizer class in program.
Fully Qualified name of StringTokenizer class is : "java.util.StringTokenizer" .

Constructors of StringTokenizer class

There are 3 constructors defined in the StringTokenizer class.

//how to create the object of StringTokenizer class
 StringTokenizer st = new StringTokenizer("Programming is fun");  
StringTokenizer st = new StringTokenizer("Programming is fun" , "  ");  
Constructor Description
public StringTokenizer(String str) Creates StringTokenizer with specified string and sets ' ' (space) as a default delimiter.
StringTokenizer(String str, String delim) creates StringTokenizer with specified string and delimeter.
StringTokenizer(String str, String delimiter, boolean returnValue) creates StringTokenizer with specified string, delimeter and returnValue. If return value is true, delimiter characters are considered to be tokens. If it is false, delimiter characters serve to separate tokens.

Methods of StringTokenizer class

The 6 useful methods of StringTokenizer class are as follows:

//Example of creating StringTokenizer object and printing each word of  string.

//to use StringTokenizer class import the java.util package first.
import java.util.StringTokenizer;

class Example {

    public static void main(String[] args) {

        //creating the object of StringTokenizer class
        StringTokenizer st = new StringTokenizer("Programming is fun");

        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            System.out.println(word);
        }
    }
}
Programming
is
fun
Method Description Syntax
hasMoreTokens() checks and returns true if there are more tokens available from this tokenizer's string otherwise returns false. public boolean hasMoreTokens()
nextToken() returns the next token from the StringTokenizer object. public String nextToken()
nextToken(String delim) returns the next token based on the delimeter. public String nextToken(String delim)
hasMoreElements() same as hasMoreTokens() method. public boolean hasMoreElements()
nextElement() same as nextToken() but its return type is Object. public Object nextElement()
countTokens() returns the total number of tokens. It returns the number of available words/tokens in StringTokenizer Object.