NoSuchElementException will be thrown if there are no more tokens. This is caused by calling nextInt () without checking for an integer. This exception is thrown to indicate that there are no more elements in the enumeration. This exception extends the RuntimeException class and therefore applies to those exceptions that can be thrown while the Java Virtual Machine (JVM) is running. This is an unchecked exception and therefore does not need to be declared in the throwers constructors method or clause. Finally, NoSuchElementException exists since the first version of Java.
java.util.NoSuchElementException is a RuntimeException that can be thrown by various classes in Java, for example, Iterator, Enumerator, Scanner or StringTokenizer.
According to the Javadoc, a NoSuchElementException is raised by the NextElement method of an enumeration to indicate that there are no more elements in the enumeration.
To prevent this, you can use hasNext () to check if more tokens are available.
source share