Recall & Review
beginner
What Java class is commonly used to read integer input from the user?The <strong>Scanner</strong> class is commonly used to read integer input from the user in Java.Click to reveal answer
beginner
How do you create a Scanner object to read input from the keyboard?
You create it with:
Scanner scanner = new Scanner(System.in);Click to reveal answer
beginner
Which Scanner method reads an integer value from the user?
The method
nextInt() reads an integer value from the user.Click to reveal answer
intermediate
What happens if the user enters a non-integer value when using
nextInt()?An InputMismatchException is thrown because the input is not an integer.
Click to reveal answer
intermediate
Why should you close the Scanner object after use?
Closing the Scanner frees system resources associated with it. Use
scanner.close(); when done.Click to reveal answer
Which import statement is needed to use Scanner in Java?
✗ Incorrect
Scanner is part of the java.util package, so you import it with
import java.util.Scanner;.What does
nextInt() do in Scanner?✗ Incorrect
nextInt() reads the next integer value from the input.How do you create a Scanner to read from the keyboard?
✗ Incorrect
System.in represents keyboard input, so you create Scanner with
new Scanner(System.in);.What exception occurs if input is not an integer when calling
nextInt()?✗ Incorrect
If input is not an integer, Scanner throws InputMismatchException.
Why is it good practice to close a Scanner after use?
✗ Incorrect
Closing Scanner frees system resources like input streams.
Explain how to read an integer input from the user in Java using Scanner.
Think about the steps from importing Scanner to reading and closing it.
You got /5 concepts.
What should you do if the user enters invalid input when expecting an integer?
Consider how to handle errors gracefully when reading input.
You got /4 concepts.