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;
✗ Incorrect
Scanner is part of the java.util package, so you import it with import 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
✗ Incorrect
nextInt() reads the next integer value from the 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);
✗ 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()?
AInputMismatchException
BNullPointerException
CNumberFormatException
DIOException
✗ Incorrect
If input is not an integer, Scanner throws InputMismatchException.
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
✗ 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.
Practice
(1/5)
1.
Which Java class is commonly used to read integer input from the user?
easy
A. FileReader
B. BufferedReader
C. Scanner
D. PrintWriter
Solution
Step 1: Identify the class for input reading
The Scanner class is designed to read input from various sources including user input.
Step 2: Confirm the class for integer input
Scanner provides the nextInt() method to read integers specifically.
Final Answer:
Scanner -> Option C
Quick Check:
Input class = Scanner [OK]
Hint: Use Scanner class to read integers easily [OK]
Common Mistakes:
Confusing Scanner with FileReader which reads files
Using PrintWriter which is for output, not input
Thinking BufferedReader reads integers directly without parsing
2.
Which of the following is the correct way to create a Scanner object to read from the keyboard?
?
easy
A. Scanner input = new Scanner(System.console());
B. Scanner input = new Scanner(System.out);
C. Scanner input = new Scanner(System.err);
D. Scanner input = new Scanner(System.in);
Solution
Step 1: Understand input source for Scanner
To read user input from the keyboard, System.in is used as the input stream.
Step 2: Match correct Scanner constructor
The correct syntax is new Scanner(System.in). Other options use output or error streams, which are incorrect.
Final Answer:
Scanner input = new Scanner(System.in); -> Option D
Quick Check:
Keyboard input = System.in [OK]
Hint: Use System.in with Scanner for keyboard input [OK]
Common Mistakes:
Using System.out or System.err instead of System.in
Trying to use System.console() which returns Console, not InputStream
Forgetting to import java.util.Scanner
3.
What will be the output of the following Java code if the user enters 42?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(num + 10);
sc.close();
}
}
medium
A. 52
B. 42
C. 10
D. Error at runtime
Solution
Step 1: Read integer input
The program reads an integer from the user using nextInt(). User inputs 42, so num = 42.
Step 2: Add 10 and print result
The program prints num + 10, which is 42 + 10 = 52.
Final Answer:
52 -> Option A
Quick Check:
42 + 10 = 52 [OK]
Hint: Add 10 to input number and print [OK]
Common Mistakes:
Printing input without adding 10
Forgetting to close Scanner (not an error but good practice)
Assuming input is string, causing error
4.
Identify the error in the following code snippet that reads an integer from the user:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextLine();
System.out.println(number);
sc.close();
}
}
medium
A. Scanner object is not closed properly.
B. Using nextLine() to read an integer causes a type mismatch error.
C. Missing import statement for Scanner.
D. System.out.println() cannot print integers.
Solution
Step 1: Check method used to read input
The code uses nextLine() which returns a String, but tries to assign it to an int variable.
Step 2: Understand type mismatch
Assigning a String to an int variable causes a compile-time error. The correct method to read an int is nextInt().
Final Answer:
Using nextLine() to read an integer causes a type mismatch error. -> Option B
Quick Check:
nextLine() returns String, not int [OK]
Hint: Use nextInt() to read integers, not nextLine() [OK]
Common Mistakes:
Using nextLine() instead of nextInt() for integers
Ignoring type mismatch errors
Not closing Scanner (though not the main error here)
5.
You want to read two integers from the user and print their sum. Which code snippet correctly does this?
?
hard
A. Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
sc.close();
B. Scanner sc = new Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = sc.nextLine();
System.out.println(a + b);
C. Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
System.out.println(a + b);
sc.close();
D. Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
String b = sc.nextLine();
System.out.println(a + b);
sc.close();
Solution
Step 1: Identify the correct code for reading two integers
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
sc.close(); uses sc.nextInt() twice to read integers directly, handling whitespace or newlines, then prints their sum.
Step 2: Eliminate incorrect options
A: Compile error - int b = sc.nextLine(); (String to int). C: Concatenates strings ("1" + "2" = "12"). D: nextLine() after nextInt() reads leftover newline (b = "", e.g., 5 + "" = "5").
Final Answer:
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
sc.close(); -> Option A
Quick Check:
Two nextInt() reads ints correctly [OK]
Hint: Use nextInt() for integer input reliably [OK]
Common Mistakes:
Mixing nextInt() and nextLine() causing input skipping
Adding strings instead of integers
Not closing Scanner after use
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.