Java Reference
In-Depth Information
The methods nextInt and nextDouble both throw an InputMismatchException if the
input is invalid. The class InputMismatchException is a subclass of the class
NoSuchElementException , which is a subclass of the class RuntimeException .The
class InputMismatchException has only two constructors, as described in Table 11-6.
TABLE 11-6 class InputMismatchException and its Constructors
public InputMismatchException()
//Default constructor
//Creates a new instance of the class InputMismatchException.
//The error message is null.
public InputMismatchException(String str)
//Constructor with parameters
//Creates a new instance of the class InputMismatchException. The
//parameter str specifies the message string that is to be retrieved
//by the method getMessage.
Tables 11-7 and 11-8 show the exceptions thrown by the methods next and nextLine
of the class Scanner .
TABLE 11-7 Exceptions Thrown by the Method next
Exception Thrown
Description
NoSuchElementException
If there is no more input (tokens)
IllegalStateException
If this scanner is closed
TABLE 11-8 Exceptions Thrown by the Method nextLine
1
1
Exception Thrown
Description
NoSuchElementException
If the input is exhausted
IllegalStateException
If this scanner is closed
In an end-of-file (EOF)-controlled while loop, we use the method hasNext of the
class Scanner to determine whether there is a token in the input stream. Table 11-9
shows the exception thrown by the method hasNext .
TABLE 11-9 Exceptions Thrown by the Method hasNext
Exception Thrown
Description
IllegalStateException
If this scanner is closed
Search WWH ::




Custom Search