We use reading integer input to get numbers from the user while the program is running. This helps the program work with data the user provides.
Reading integer input in Java
import java.util.Scanner; Scanner scanner = new Scanner(System.in); int number = scanner.nextInt();
You must import java.util.Scanner to read input.
nextInt() reads the next whole number typed by the user.
age.Scanner scanner = new Scanner(System.in); int age = scanner.nextInt();
num.Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int num = scanner.nextInt();
This program asks the user to type an integer, reads it, and then prints it back.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Please enter an integer: "); int userNumber = scanner.nextInt(); System.out.println("You entered: " + userNumber); scanner.close(); } }
Always close the Scanner after use with scanner.close() to free resources.
If the user types something that is not an integer, the program will throw an error.
Use Scanner and nextInt() to read integer input from the user.
Remember to import java.util.Scanner and close the scanner when done.
Reading input lets your program interact with the user by using their numbers.
Practice
Which Java class is commonly used to read integer input from the user?
Solution
Step 1: Identify the class for input reading
TheScannerclass is designed to read input from various sources including user input.Step 2: Confirm the class for integer input
Scannerprovides thenextInt()method to read integers specifically.Final Answer:
Scanner -> Option CQuick Check:
Input class = Scanner [OK]
- Confusing Scanner with FileReader which reads files
- Using PrintWriter which is for output, not input
- Thinking BufferedReader reads integers directly without parsing
Which of the following is the correct way to create a Scanner object to read from the keyboard?
?
Solution
Step 1: Understand input source for Scanner
To read user input from the keyboard,System.inis used as the input stream.Step 2: Match correct Scanner constructor
The correct syntax isnew Scanner(System.in). Other options use output or error streams, which are incorrect.Final Answer:
Scanner input = new Scanner(System.in); -> Option DQuick Check:
Keyboard input = System.in [OK]
- 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
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();
}
}Solution
Step 1: Read integer input
The program reads an integer from the user usingnextInt(). User inputs 42, sonum = 42.Step 2: Add 10 and print result
The program printsnum + 10, which is 42 + 10 = 52.Final Answer:
52 -> Option AQuick Check:
42 + 10 = 52 [OK]
- Printing input without adding 10
- Forgetting to close Scanner (not an error but good practice)
- Assuming input is string, causing error
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();
}
}Solution
Step 1: Check method used to read input
The code usesnextLine()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 isnextInt().Final Answer:
Using nextLine() to read an integer causes a type mismatch error. -> Option BQuick Check:
nextLine() returns String, not int [OK]
- Using nextLine() instead of nextInt() for integers
- Ignoring type mismatch errors
- Not closing Scanner (though not the main error here)
You want to read two integers from the user and print their sum. Which code snippet correctly does this?
?
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(); usessc.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()afternextInt()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 AQuick Check:
Two nextInt() reads ints correctly [OK]
- Mixing nextInt() and nextLine() causing input skipping
- Adding strings instead of integers
- Not closing Scanner after use
Ready to prove it?
Pass the test and this pattern moves unknown
