
310 CHAPTER 6 Flow of Control,Part 2: Looping
Figure 6.11
Roll a Ball,Version 2
6.5 Type-Safe Input Using Scanner
One problem with reading input using Scanner is that if the next token
does not match the data type we expect, an InputMismatchException is gen-
erated, which stops execution of the program. This could be caused by a
simple typo on the user’s part; for example, the user may type a letter or
other nonnumeric character when our program prompts for an integer. To
illustrate this problem, Example 6.9 shows a small program that prompts
the user for an integer and calls the nextInt method of the Scanner class to
read the integer, and Figure 6.12 ...