Enumeration hasMoreElements() Method in Java with Examples

An object that implements the Enumeration interface generates a series of elements, one at a time. hasMoreElements() method of Enumeration used to tests if this enumeration contains more elements. If enumeration contains more element then it will return true else false.

Syntax:

boolean hasMoreElements()

Parameters: This method accepts nothing.



Return value: This method returns true if and only if this enumeration object contains at least one more element to provide; false otherwise.

Below programs illustrate hasMoreElements() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Enumeration.hasMoreElements() method
  
import java.util.*;
  
public class GFG {
  
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args)
    {
  
        Enumeration Days;
        Vector week = new Vector();
  
        week.add("Sunday");
        week.add("Monday");
        week.add("Tuesday");
        week.add("Wednesday");
        week.add("Thursday");
        week.add("Friday");
        week.add("Saturday");
        Days = week.elements();
  
        while (Days.hasMoreElements()) {
            System.out.println("Day = "
                               + Days.nextElement());
        }
    }
}

chevron_right


Output:

Day = Sunday
Day = Monday
Day = Tuesday
Day = Wednesday
Day = Thursday
Day = Friday
Day = Saturday

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Enumeration.hasMoreElements() method
  
import java.util.*;
  
public class GFG {
  
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args)
    {
  
        Enumeration<Integer> classNine;
        Vector<Integer> rollno = new Vector<Integer>();
  
        rollno.add(1);
        rollno.add(2);
        rollno.add(3);
        rollno.add(4);
        rollno.add(5);
        rollno.add(6);
        rollno.add(7);
        rollno.add(8);
        classNine = rollno.elements();
  
        while (classNine.hasMoreElements()) {
            System.out.println("Roll No = "
                               + classNine.nextElement());
        }
    }
}

chevron_right


Output:

Roll No = 1
Roll No = 2
Roll No = 3
Roll No = 4
Roll No = 5
Roll No = 6
Roll No = 7
Roll No = 8

References: https://docs.oracle.com/javase/10/docs/api/java/util/Enumeration.html#hasMoreElements()




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.