0
0
Javaprogramming~5 mins

Reading integer input in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aimport java.io.Scanner;
Bimport java.input.Scanner;
Cimport java.lang.Scanner;
Dimport java.util.Scanner;
What does nextInt() do in Scanner?
AReads the next integer from input
BReads the next string from input
CReads the next line from input
DReads the next double from input
How do you create a Scanner to read from the keyboard?
AScanner scanner = new Scanner(System.out);
BScanner scanner = new Scanner(System.in);
CScanner scanner = new Scanner(System.err);
DScanner scanner = new Scanner(System.input);
What exception occurs if input is not an integer when calling nextInt()?
AInputMismatchException
BNullPointerException
CNumberFormatException
DIOException
Why is it good practice to close a Scanner after use?
ATo prevent syntax errors
BTo clear the input buffer
CTo free system resources
DTo reset the Scanner object
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.