Java StringTokenizer Class

StringTokenizer class

The StringTokenizer class exists in the java.util package and allows an application to break the string into tokens by specifying a delimiter. The tokenization methods of StringTokenizer is much simpler than the one used in the StreamTokenizer class.

Unlike StreamTokenizer, StringTokenizer does not differentiate among identifiers, numbers and quoted strings and dord not even recognize and skip comments.

Let's see a simple example to see what this class can do.

Example

In this example a string is tokenized on the basis of commas, white spaces and hyphens.

  1. import java.util.StringTokenizer;  
  2.   
  3. public class Demo{  
  4.  public static void main(String args[]){  
  5.    StringTokenizer str = new StringTokenizer("This is demo of white space"," ");  
  6.      while (str.hasMoreTokens()) {  
  7.          System.out.println(str.nextToken());  
  8.      }  
  9.    System.out.println();  
  10.          StringTokenizer str1 = new StringTokenizer("This,is,demo,of,comma"",");  
  11.               while (str1.hasMoreTokens()) {  
  12.          System.out.println(str1.nextToken());  
  13.               }  
  14.          System.out.println();  
  15.               StringTokenizer str2 = new StringTokenizer("This-is-demo-of-hyphen""-");  
  16.               while (str2.hasMoreTokens()) {  
  17.          System.out.println(str2.nextToken());  
  18.      }  
  19.    }  
  20. }  
Output

tokenized

Constructors used

Constructors


There are three constructors used by the StringTokenizer class as in the following.

 

  • StringTokenizer(String str): Constructs a StringTokenizer with the specified string.
  • StringTokenizer(String str, String delim): Constructs a StringTokenizer with the specified string and delimiter.
  • StringTokenizer(String str, String delim, Boolean returnDelim): Constructs a StringTokenizer with the specified string, delimiter and returnDelimiter value. If the value is true then the delimiter characters is considered to be the token otherwise characters serve to separate tokens.

Methods used

There are six methods used by the StringTokenizer class that are as in the following.

Methods

Let's explain all the methods with simple examples.

String nextTokens()

This method returns the next token from the StringTokenizer object. It throws NoSuchElementException() if there are no more tokens available in the StringTokenizer object.

Example

  1. import java.util.*;  
  2.   
  3. public class Demo {  
  4.    public static void main(String[] args) {  
  5.    // creating string tokenizer  
  6.    StringTokenizer str = new StringTokenizer("Learn,code and share...");  
  7.   
  8.    // checking next token  
  9.    System.out.println("Next token is : " + str.nextToken());  
  10.    str = new StringTokenizer("One two and three");  
  11.    System.out.println("Next token is : " + str.nextToken());  
  12.       str = new StringTokenizer("123-456-789 000");  
  13.    System.out.println("Next token is : " + str.nextToken());  
  14.    }  
  15. }  
Output

String nextTokens image

In the preceding output, you can see that the words linked in some way are also printed as the next token and that are not linked, like separated by white space, are not considered to be the next token.

String nextTokens(String delim)

This method returns the next token based on the delimiter. In other words, first of all the set of characters considered to be delimiters by the StringTokenizer object is changed to be the characters in string delim. Then the next token in the given string after the current position is returned.

It also throws NoSuchElementException() if there are no more tokens available in the StringTokenizer object.

Example
  1. import java.util.*;  
  2.   
  3. public class Demo {  
  4.    public static void main(String[] args) {  
  5.    // creating string tokenizer with delimeter  
  6.    StringTokenizer str = new StringTokenizer("Apple/Mongo/Csharp/Database/EJB");  
  7.   
  8.    // checking next token  
  9.    System.out.println("Next token is : " + str.nextToken("/"));  
  10.    str = new StringTokenizer("Apple-Mongo-Csharp-Database-EJB");  
  11.    System.out.println("Next token is : " + str.nextToken("-"));  
  12.    str = new StringTokenizer("Apple,Mongo,Csharp,Database,EJB");  
  13.    System.out.println("Next token is : " + str.nextToken(","));  
  14.    str = new StringTokenizer("Apple Mongo Csharp Database EJB");  
  15.    System.out.println("Next token is : " + str.nextToken(" "));  
  16.    }  
  17. }  
Output

String nextTokens

Object nextElement()

This method is approximately similar to that of the nextToken() method, but with the difference that it returns an object instead of a string. It also throws NoSuchElementException() if there are no more tokens available in the StringTokenizer object.

Example
  1. import java.util.*;  
  2.   
  3. public class Demo {  
  4.    public static void main(String[] args) {  
  5.    // creating string tokenizer  
  6.    StringTokenizer str = new StringTokenizer("Join us on social networks");  
  7.   
  8.    // moving to next element  
  9.    str.nextElement();  
  10.   
  11.    // checking next to next element  
  12.    System.out.println("Next element is : " + str.nextElement());  
  13.      
  14.       str = new StringTokenizer("Hello how are you.?");  
  15.       str.nextElement();  
  16.    System.out.println("Next element is : " + str.nextElement());  
  17.   
  18.    //throws exception  
  19.    str = new StringTokenizer("Join-us-on-social-networks");  
  20.       str.nextElement();  
  21.    System.out.println("Next element is : " + str.nextElement());  
  22.    }  
  23. }  
Output

Object nextElement

In the preceding output, you can see that for the top two methods it gives a proper result, but for the third it treats the entire string as a single string because all words in the string are attached with a hyphen. That's why the third method is unable to find the next token.

int countTokens()

This method returns the total number of tokens. In other words, it calculate the number of times the tokenizer's nextToken() method can be called before generating the exception.

Example
  1. import java.util.*;  
  2.   
  3. public class Demo {  
  4.    public static void main(String[] args) {  
  5.    // creating string tokenizer  
  6.    StringTokenizer str = new StringTokenizer("NEWS - North East West South");  
  7.   
  8.    // counting tokens  
  9.    System.out.println("Total tokens : " + str.countTokens());  
  10.   
  11.    str = new StringTokenizer("Left,Right,Top,Bottom");  
  12.    System.out.println("Total tokens : " + str.countTokens());  
  13.       str = new StringTokenizer("Left , Right , Top , Bottom");  
  14.    System.out.println("Total tokens : " + str.countTokens());  
  15.    }  
  16. }  
Output

int countTokens

boolean hasMoreTokens()

This method is used to check whether or not the StringTokenizer contains more tokens. The method call returns “true” only if there is at least one token available in the tokenizer's string after the current position, otherwise it returns false.

Example
  1. import java.util.*;  
  2.   
  3. public class Demo {  
  4.    public static void main(String[] args) {  
  5.    // creating string tokenizer  
  6.    StringTokenizer str = new StringTokenizer("Life has four days to live");  
  7.   
  8.    // counting tokens  
  9.    System.out.println("Total tokens : " + str.countTokens());  
  10.   
  11.    // checking tokens  
  12.    while (str.hasMoreTokens()){  
  13.    System.out.println("Next token : " + str.nextToken());  
  14.    }  
  15.    System.out.println();  
  16.    str = new StringTokenizer("123-456 789/007");  
  17.    System.out.println("Total tokens : " + str.countTokens());  
  18.       while (str.hasMoreTokens()){  
  19.    System.out.println("Next token : " + str.nextToken());  
  20.    }  
  21.    }  
  22. }  
Output

boolean hasMoreTokens

boolean hasMoreElements()

This method is quite similar to the hasMoreTokens() method. The only purpose of its existence is that the class can implement an Enumeration interface.

Example
  1. import java.util.*;  
  2.   
  3. public class Demo {  
  4.    public static void main(String[] args) {  
  5.    // creating string tokenizer  
  6.    StringTokenizer str = new StringTokenizer("You are right");  
  7.   
  8.    // checking elements  
  9.    while (str.hasMoreElements()){  
  10.    System.out.println("Next element : " + str.nextElement());  
  11.    }  
  12.    System.out.println();  
  13.    str = new StringTokenizer("979 415 642");  
  14.       while (str.hasMoreElements()){  
  15.    System.out.println("Next element : " + str.nextElement());  
  16.    }  
  17.    }  
  18. }  
Output

Output

Thank you, keep learning and sharing.

Up Next
    Ebook Download
    View all
    Learn
    View all