StringTokenizer hasMoreTokens() Method in Java with Examples

The hasMoreTokens() method of StringTokenizer class checks whether there are any more tokens available with this StringTokenizer.

Syntax:

public boolean hasMoreTokens()

Parameters: The method does not take any parameters.



Return Value: The method returns boolean True if the availability of at least one more token is found in the string after the current position else false.

Below programs illustrate the working of hasMoreTokens() Method of StringTokenizer:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate hasMoreTokens() method
  
import java.util.*;
  
public class StringTokenizer_Demo {
    public static void main(String args[])
    {
        // Creating a StringTokenizer
        StringTokenizer str_arr
            = new StringTokenizer(
                "Lets practice at GeeksforGeeks");
  
        // Counting the tokens
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens());
  
        // Checking for any tokens
        System.out.println(str_arr.hasMoreTokens());
  
        // Checking and displaying the Tokens
        while (str_arr.hasMoreTokens()) {
            System.out.println("The Next token: "
                               + str_arr.nextToken());
        }
    }
}

chevron_right


Output:

The number of Tokens are: 4
true
The Next token: Lets
The Next token: practice
The Next token: at
The Next token: GeeksforGeeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate hasMoreTokens() method
  
import java.util.*;
  
public class StringTokenizer_Demo {
    public static void main(String args[])
    {
        // Creating a StringTokenizer
        StringTokenizer str_arr
            = new StringTokenizer("");
  
        // Counting the tokens
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens());
  
        // Checking for any tokens
        System.out.println(str_arr.hasMoreTokens());
    }
}

chevron_right


Output:

The number of Tokens are: 0
false


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.