Completion requirements
Java provides a Scanner class to facilitate data input/output. In this section, you will learn about the Scanner class that is used to get input from the user. Java also defines various methods from the Scanner class that can convert user input into appropriate data types before conducting any operation on the data.
15. InputMismatchException
Answer:
Enter an integer: 1492 OK
Enter an integer: Fourteen ninety two WRONG
Enter an integer: 14.92 WRONG
Enter an integer: -1492 OK
Enter an integer: 1 4 9 2 MAYBE , but only the '1' will be scanned
InputMismatchException
InputMismatchException
If the user enters one of the WRONG lines, the statement
num = scan.nextInt()
will not be able to scan the characters as an integer. It will throw an exception. You will see something like the above.
This exception was passed out of the running program to the Java system, which stopped the program and wrote the error messages.
Question 15:
At what line of your program did the problem occur? (Inspect the error message from the program.)