InputMismatchException

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

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 — only the '1' will be scanned

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 with throw an exception. The user will see something like:

input mismatch exception

This exception was passed out of the running program to the Java system, which stopped the program and wrote the error messages.

QUESTION 15:

(Trick Question: ) Can Scanner.nextInt() read the following as an integer?

+84