Saturday, September 27, 2025
HomeLanguagesJavaStringTokenizer hasMoreElements() Method in Java with Examples

StringTokenizer hasMoreElements() Method in Java with Examples

The hasMoreElements() method of StringTokenizer class also checks whether there are any more tokens available with this StringTokenizer. It is similar to the hasMoreTokens(). The method exists exclusively so that the Enumeration interface of this class can be implemented.

Syntax:

public boolean hasMoreElements()

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 hasMoreElements() Method of StringTokenizer:
Example 1:




// Java code to illustrate hasMoreElements() 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 Lazyroar");
  
        // Counting the tokens
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens());
  
        // Checking for any tokens
        System.out.println(str_arr.hasMoreElements());
  
        // Checking and displaying the Tokens
        while (str_arr.hasMoreElements()) {
            System.out.println("The Next token: "
                               + str_arr.nextToken());
        }
    }
}


Output:

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

Example 2:




// Java code to illustrate hasMoreElements() 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.hasMoreElements());
    }
}


Output:

The number of Tokens are: 0
false
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32323 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11913 POSTS0 COMMENTS
Shaida Kate Naidoo
6806 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS