Coding Conundrum The following code has been written to prompt the user for his or her age. However, when the program is compiled, an error is reported. It is a conundrum! 1 /* Inputting an age 2 */ 3 4 5 public class Conundrum { 6 public static void main( String [ ] args ) { 7 Scanner input = new Scanner( System.in ) 8 9 // prompt and read the user’s age 10 System.out.print( “Enter your age ) 11 int age = input.nextInt( ) 12 13 System.out.println( “You are + age + years old.” ) 14 15 } 16 } Error Report Conundrum.java:7: error: cannot find symbol Scanner input = new Scanner( System.in ) ^ symbol: class Scanner location: class Conundrum Conundrum.java:7: error: cannot find symbol Scanner input = new Scanner( System.in ) ^ symbol: class Scanner location: class Conundrum 2 errors 1. What is the problem, and how can you fix the error? 2. After the error is fixed, the program is run. The user, who is 15 1/2, enters 15.5. The program stops with the following output. Why did this exception occur? Enter your age 15.5 Exception in thread “main” java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Conundrum.main(Conundrum.java:11) Copyright Goodheart-Willcox Co., Inc. 90 Introduction to Computer Science: Java Programming
Previous Page Next Page