Scanner for
Console Input
Learn how to read user input from the console using Java's Scanner class: the essential gateway to interactive programs.
What Is Console Input?
Most programs need to receive data from the user while they run. In Java, the simplest way to do this is through the console: the text-based terminal where you see program output and type responses.
Without input, every program would produce the same output every time. Input makes programs dynamic and interactive.
Introducing Scanner
Java provides a built-in class called Scanner in the java.util package. It acts as a bridge between the keyboard and your program's variables.
Why Use Scanner?
For AP Computer Science and most introductory Java courses, Scanner is the standard way to handle console input.
The Two Required Lines
Every program that uses Scanner needs exactly two setup steps before reading any input:
// Step 1: Import the Scanner class import java.util.Scanner; public class MyProgram { public static void main(String[] args) { // Step 2: Create a Scanner object Scanner input = new Scanner(System.in); } }
Core Scanner Methods
| Method | Return Type | Reads |
|---|---|---|
nextInt() |
int |
One integer |
nextDouble() |
double |
One decimal number |
next() |
String |
One word (no spaces) |
nextLine() |
String |
Entire line (with spaces) |
nextBoolean() |
boolean |
true or false |
next() vs nextLine()
This is the most important distinction for beginners. Both read String values, but they behave very differently:
"Hello"."Hello World".Step-by-Step Approach
import java.util.Scanner; at the top of your file.Scanner sc = new Scanner(System.in); inside your method.System.out.print() to tell the user what to type.nextInt(), nextLine(), etc.sc.close(); when finished to release resources.Example 1: Reading an Integer
import java.util.Scanner; public class AgeChecker { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your age: "); int age = sc.nextInt(); System.out.println("You are " + age + " years old."); sc.close(); } }
Example 2: Reading a Full Name
import java.util.Scanner; public class Greeter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your full name: "); String name = sc.nextLine(); System.out.println("Hello, " + name + "!"); sc.close(); } }
Example 3: Multiple Inputs
import java.util.Scanner; public class RectangleArea { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Width: "); double w = sc.nextDouble(); System.out.print("Height: "); double h = sc.nextDouble(); System.out.println("Area = " + (w * h)); sc.close(); } }
The nextLine() Trap
The most common Scanner bug happens when you mix nextInt() (or nextDouble()) with nextLine():
System.out.print("Age: "); int age = sc.nextInt(); // reads "25" but leaves \n System.out.print("Name: "); String name = sc.nextLine(); // reads the leftover \n! // name is now "" (empty string)
int age = sc.nextInt(); sc.nextLine(); // consume leftover newline String name = sc.nextLine(); // now reads correctly
InputMismatchException
If you call nextInt() but the user types a word like "hello", Java throws an InputMismatchException and your program crashes.
Common Mistakes
import java.util.Scanner; the compiler cannot find the Scanner class.next() when the input has spaces. Only nextLine() captures the full line.nextLine() right after nextInt() without consuming the leftover \n.Method Summary
| I need to read... | Use this method | Watch out for |
|---|---|---|
| A whole number | nextInt() |
Leftover \n |
| A decimal | nextDouble() |
Leftover \n |
| A single word | next() |
Stops at spaces |
| A full sentence | nextLine() |
Mixing with nextInt() |
| true / false | nextBoolean() |
Case-insensitive |
Question 1 of 5
Which import statement is required before using the Scanner class?
import java.util.Scanner;.Question 2 of 5
What does nextInt() return when the user types "42" and presses Enter?
nextInt() parses the input as an integer and returns the primitive int value 42.Question 3 of 5
A user types "Hello World". Which method captures the entire input including the space?
next() only reads up to the first whitespace, returning "Hello". nextLine() reads the entire line, returning "Hello World".Question 4 of 5
Consider the following code:
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
String s = sc.nextLine();
The user types 5 and presses Enter, then expects to type a name.
What value does the variable s hold after the code runs?
nextInt() reads 5 but leaves the newline \n in the buffer. nextLine() immediately consumes that leftover \n and returns an empty String. This is the classic "nextLine trap."Question 5 of 5
What happens if you call nextInt() and the user types "abc"?
nextInt() encounters non-numeric input, Java throws an InputMismatchException at runtime because "abc" cannot be parsed as an integer.