User Input (Scanner)
Most useful programs need to read input from the user rather than just working with hardcoded values. The standard way to read keyboard input from the console in Java is the Scanner class, found in java.util.
Creating a Scanner
A Scanner wraps an input source and gives you methods to read different types of values from it. To read from the keyboard, wrap System.in, which represents standard input.
Reading basic input
Java
import java.util.Scanner;
public class ScannerBasicsDemo {
public static void 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();
System.out.println("Hello, " + name + "! You are " + age + " years old.");
}
}Enter your name: Aman Enter your age: 28 Hello, Aman! You are 28 years old.
Common Scanner Methods
Method | Reads |
.nextLine() | An entire line of text (including spaces) |
.next() | A single token, up to the next whitespace |
.nextInt() | An int value |
.nextDouble() | A double value |
.nextBoolean() | A boolean value (true/false) |
.hasNext() | Whether more input is available |
Reading different types
Java
import java.util.Scanner;
public class ScannerTypesDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
System.out.print("Enter price: ");
double price = scanner.nextDouble();
double total = quantity * price;
System.out.println("Total: " + total);
}
}The Classic Bug: Mixing nextInt() and nextLine()
This is one of the most common Scanner bugs beginners run into. .nextInt() (and .nextDouble(), .nextBoolean(), etc.) reads only the numeric token itself — it does not consume the newline character left behind when the user pressed Enter. If a call to .nextLine() immediately follows, it reads that leftover, empty newline instead of waiting for new input.
Broken: nextLine() silently reads an empty line
Java
import java.util.Scanner;
public class ScannerBugDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // reads the leftover newline, not a name!
System.out.println("Age: " + age);
System.out.println("Name: [" + name + "]");
}
}Warning
If the user types 28 and presses Enter, then types Aman and presses Enter, this program prints Name: [] — an empty string. The .nextInt() call consumed only the digits 28, leaving the trailing newline character sitting in the input buffer. The very next .nextLine() immediately reads that leftover newline as an empty line, and the user never even gets a chance to type their name before the program moves on.
The fix is to consume that leftover newline with an extra, throwaway .nextLine() call right after any numeric read that will be followed by a line read.
Fixed: consume the leftover newline
Java
import java.util.Scanner;
public class ScannerFixedDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.nextLine(); // consume the leftover newline
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // now reads the actual name
System.out.println("Age: " + age);
System.out.println("Name: [" + name + "]");
}
}Enter your age: 28 Enter your name: Aman Age: 28 Name: [Aman]
Tip
An alternative fix is to read everything with .nextLine() and parse numbers yourself with Integer.parseInt() or Double.parseDouble(). Many experienced developers prefer this approach specifically because it avoids the newline trap altogether.
Closing the Scanner
Like other resources, a Scanner can be closed with .close() when you're done with it, which releases the underlying resource.
Closing a Scanner
Java
import java.util.Scanner;
public class ScannerCloseDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = scanner.nextLine();
System.out.println("You entered: " + word);
scanner.close();
}
}Note
For a small, simple program that reads from System.in once and then exits, explicitly closing the scanner is not critical — the JVM cleans everything up when the program ends. It is still considered good practice, though, especially in longer-lived programs. When you have a resource that truly needs deterministic cleanup, the idiomatic modern approach is try-with-resources, which automatically closes the resource for you — see the dedicated try-with-resources page for details.
Warning
Be careful about closing a Scanner wrapped around System.in if you plan to read more input later in the same program — closing it also closes the underlying System.in stream, and creating a second Scanner on it afterward will not work as expected.