String Tokenizer in JAVA with example

String Tokenizer class is for breaking a string into tokens. This class is basically a legacy class although it is used only because of its compatible reasons, but its use is discouraged in new code, as its methods does not able to distinguish between identifiers, numbers and strings that are quoted.

Moreover this class method does not able to recognize the comments.

String Tokenizer class implements Enumeration Interface and Extends Object Class.


String Tokenizer Constructors:

1. StringTokenizer(String str)

This constructor creates a stringTokenizer  with the specified string as mentioned in the argument list.

2. StringTokenizer(String str , String delim)

This constructor creates a stringTokenizer with the specified string and delimeter as given in the argument list.

3. StringTokenizer(String str , String delim , boolean returnValue)

This constructor creates a stringTokenizer with the specified string and delimeter as mentioned in the argument list. It has a Boolean value returnValue, if it is true delimeter characters are considered to be tokens or else if it is false delimeter characters serve to separate tokens


String Tokenizer Methods 

1. int countTokens()

This method returns the total number of tokens , as shown in the following program.

import java.util.StringTokenizer;
public class StringTokenizerMethods{
public static void main(String args[]){
StringTokenizer stringTokenizer = new StringTokenizer("Welcome to AbhiAndroid");

System.out.println("Total Tokens: " + stringTokenizer.countTokens());
}
}

Output:

Total Tokens: 3

2. boolean hasMoreElements()

This methods checks if there are more elements present or not. It returns true if there are, else returns false, as shown in the following program.

import java.util.StringTokenizer;
public class StringTokenizerMethods{
public static void main(String args[]){

StringTokenizer stringTokenizer = new StringTokenizer("Welcome to AbhiAndroid");

while(stringTokenizer.hasMoreElements()){

System.out.println(stringTokenizer.nextElement());
}
}
}

Output:

Welcome

to

AbhiAndroid

3. boolean hasMoreTokens()

This methods checks if there are more tokens present or not. It returns true if there are, else returns false, as shown in the following program. 

import java.util.StringTokenizer;
public class StringTokenizerMethods{
public static void main(String args[]){

StringTokenizer stringTokenizer = new StringTokenizer("Welcome to AbhiAndroid");

while(stringTokenizer.hasMoreTokens()){
System.out.println(stringTokenizer.nextToken());
}
}
}

Output:

Welcome to AbhiAndroid

4. Object nextElement()

This method returns the next element from the StringTokenizer Object, as shown in the following program.

import java.util.StringTokenizer;
public class StringTokenizerMethods{
public static void main(String args[]){

StringTokenizer stringTokenizer = new StringTokenizer("Welcome to AbhiAndroid");

while(stringTokenizer.hasMoreElements()){
System.out.println(stringTokenizer.nextElement());
}
}
}

 Output:

Welcome

to

AbhiAndroid

5. String nextToken()

This method returns the next token from the StringTokenizer Object, as shown in the following program.

import java.util.StringTokenizer;
public class StringTokenizerMethods{
public static void main(String args[]){
StringTokenizer stringTokenizer = new StringTokenizer("Welcome to AbhiAndroid");

while(stringTokenizer.hasMoreTokens()){
System.out.println(stringTokenizer.nextToken());
}
}
}

 Output:

Welcome

to

AbhiAndroid

6. String nextToken(String delim)

This method returns the next token from the StringTokenizer Object based on the delimeter as given in the argument list, as shown in the following program.

import java.util.StringTokenizer;
public class StringTokenizerMethods{
public static void main(String args[]){

StringTokenizer stringTokenizer = new StringTokenizer("Welcome-to-AbhiAndroid");

while(stringTokenizer.hasMoreTokens()){
System.out.println(stringTokenizer.nextToken("-"));
}
}
}

Output:

Welcome

to

AbhiAndroid