The Scanner class helps your program read input from the keyboard or other sources easily.
When you want to ask the user to type their name.
When you need to get numbers or words from the user during the program.
When you want to read data from a file or text input.
When you want to make your program interactive by accepting user input.
Syntax
Java
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
// To read a stringString text = scanner.nextLine();
// To read an integerint number = scanner.nextInt();
// Close scanner when done
scanner.close();
Always import java.util.Scanner before using it.
Remember to close the scanner with scanner.close() to free resources.
Examples
This reads a full line of text from the user and stores it in name.
Java
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
This reads an integer number from the user and stores it in age.
Java
Scanner scanner = new Scanner(System.in);
int age = scanner.nextInt();
This reads a decimal number from the user and stores it in price.
Java
Scanner scanner = new Scanner(System.in);
double price = scanner.nextDouble();
Sample Program
This program asks the user to type their name and age, then prints a greeting message using that information.
Java
import java.util.Scanner;
publicclass InputExample {
publicstaticvoid main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.nextLine(); // consume the leftover newlineSystem.out.println("Hello, " + name + ". You are " + age + " years old.");
scanner.close();
}
}
OutputSuccess
Important Notes
If you use nextInt() and then nextLine(), you may need to add an extra scanner.nextLine() to consume the leftover newline.
Scanner can read different types like nextDouble(), nextBoolean(), etc.
Summary
The Scanner class reads user input easily.
Use different methods like nextLine() or nextInt() depending on the input type.
Always close the Scanner when done to avoid resource leaks.
Practice
(1/5)
1. What is the primary purpose of the Scanner class in Java?
easy
A. To write output to the console
B. To handle file operations
C. To create graphical user interfaces
D. To read input from the user
Solution
Step 1: Understand Scanner's role
The Scanner class is designed to read input from various sources, commonly from the user via the keyboard.
Step 2: Differentiate from other classes
Writing output is done by System.out, GUI creation uses other libraries, and file handling uses classes like FileReader.
Final Answer:
To read input from the user -> Option D
Quick Check:
Scanner reads input = B [OK]
Hint: Scanner reads user input from keyboard easily [OK]
Common Mistakes:
Confusing Scanner with output classes
Thinking Scanner creates GUI
Assuming Scanner handles files only
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.in);
B. Scanner input = new Scanner(System.out);
C. Scanner input = new Scanner(System.err);
D. Scanner input = new Scanner(System.in.read());
Solution
Step 1: Identify correct Scanner constructor
The Scanner constructor requires an InputStream to read input. System.in is the standard input stream (keyboard).
Step 2: Check other options
System.out and System.err are output streams, not input. System.in.read() returns an int, not an InputStream.
Final Answer:
Scanner input = new Scanner(System.in); -> Option A
Quick Check:
Use System.in for input Scanner [OK]
Hint: Use System.in to read keyboard input with Scanner [OK]
Common Mistakes:
Using System.out or System.err instead of System.in
Passing System.in.read() which is an int, not InputStream
Missing 'new' keyword
3. What will be the output of the following code if the user inputs the number 25?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(num * 2);
sc.close();
}
}
medium
A. Error at runtime
B. 25
C. 50
D. 0
Solution
Step 1: Understand input reading
The program reads an integer from the user using nextInt(). User inputs 25, so num = 25.
Step 2: Calculate output
The program prints num * 2, which is 25 * 2 = 50.
Final Answer:
50 -> Option C
Quick Check:
Input 25 * 2 = 50 [OK]
Hint: Multiply input number by 2 to get output [OK]
Common Mistakes:
Confusing input with output
Forgetting to multiply by 2
Not closing Scanner (though not causing output error)
4. Identify the error in the following code snippet:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:");
String name = sc.nextInt();
System.out.println("Hello, " + name);
sc.close();
}
}
medium
A. Not closing the Scanner
B. Using nextInt() to read a String
C. Missing import statement for Scanner
D. Incorrect print statement syntax
Solution
Step 1: Check input method used
The code uses nextInt() to read the user's name, which is a String, not an integer.
Step 2: Identify correct method
To read a String, nextLine() or next() should be used instead of nextInt().
Final Answer:
Using nextInt() to read a String -> Option B
Quick Check:
Use nextLine() for String input, not nextInt() [OK]
Hint: Use nextLine() for strings, nextInt() for numbers [OK]
Common Mistakes:
Using nextInt() for String input
Ignoring Scanner import
Not closing Scanner (not error here)
Misunderstanding print syntax
5. You want to read a full line of text including spaces from the user after reading an integer using Scanner. Which sequence of method calls correctly handles this without skipping input?
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
// What next?
hard
A. sc.nextLine(); String name = sc.nextLine();
B. String name = sc.nextLine();
C. sc.next(); String name = sc.nextLine();
D. sc.nextInt(); String name = sc.nextLine();
Solution
Step 1: Understand Scanner behavior after nextInt()
After nextInt(), the newline character remains in the input buffer, so a direct nextLine() reads that leftover newline.
Step 2: Consume leftover newline before reading full line
Calling sc.nextLine() once consumes the leftover newline, then the next nextLine() reads the actual full line input.
Final Answer:
sc.nextLine(); String name = sc.nextLine(); -> Option A
Quick Check:
Consume newline with nextLine() before reading full line [OK]
Hint: Call nextLine() twice after nextInt() to read full line [OK]
Common Mistakes:
Skipping the first nextLine() to consume newline
Using next() which reads only one word
Calling nextInt() again unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.