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:
// 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()); } } } |
Day = Sunday Day = Monday Day = Tuesday Day = Wednesday Day = Thursday Day = Friday Day = Saturday
Program 2:
// 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()); } } } |
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()