Java User Input with Scanner
In this lesson
Programs become interesting when they react to the user. In Java, the easiest way to read keyboard input is the Scanner class.
Reading input with Scanner#
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Your name: ");
String name = in.nextLine();
System.out.print("Your age: ");
int age = in.nextInt();
System.out.println("Hi " + name + ", next year you'll be " + (age + 1));
in.close();
}
}
Your name: Asha Kumar
Your age: 21
Hi Asha Kumar, next year you'll be 22
import java.util.Scanner;brings the class in (it’s not injava.lang).new Scanner(System.in)reads from the keyboard.
| Method | Reads |
|---|---|
nextLine() | the whole line (with spaces) |
next() | one word (up to a space) |
nextInt() | an int |
nextDouble() | a double |
nextBoolean() | true/false |
hasNextInt() | checks if the next token is an int (doesn’t read) |
The nextInt() + nextLine() trap#
System.out.print("Age: ");
int age = in.nextInt(); // user types 21 and presses Enter
System.out.print("City: ");
String city = in.nextLine(); // returns "" immediately!
nextInt() reads 21 but leaves the Enter (newline) behind. The next nextLine() reads that empty leftover. Fix: call in.nextLine(); once after nextInt() to consume it, or read every line with nextLine() and convert:
int age = Integer.parseInt(in.nextLine().trim());
Reading everything with nextLine() and converting yourself is the safest habit. It avoids the trap completely.
Validating input#
If the user types abc where you expect a number, nextInt() throws InputMismatchException. Check first:
import java.util.Scanner;
public class SafeInput {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter marks (0-100): ");
while (!in.hasNextInt()) {
System.out.print("Numbers only, try again: ");
in.next(); // throw away the bad token
}
int marks = in.nextInt();
System.out.println(marks >= 40 ? "Pass" : "Fail");
}
}
Printing with printf#
printf formats output neatly, great for tables and bills:
public class Bill {
public static void main(String[] args) {
String[] items = {"Notebook", "Pen set", "Backpack"};
double[] prices = {120, 250.5, 1899};
double total = 0;
System.out.printf("%-10s %10s%n", "Item", "Price");
for (int i = 0; i < items.length; i++) {
System.out.printf("%-10s %10.2f%n", items[i], prices[i]);
total += prices[i];
}
System.out.printf("%-10s %10.2f%n", "TOTAL", total);
}
}
Item Price
Notebook 120.00
Pen set 250.50
Backpack 1899.00
TOTAL 2269.50
| Format | Meaning |
|---|---|
%d | integer |
%f, %.2f | decimal, 2 decimal places |
%s | String |
%c | char |
%n | new line (works on every OS) |
%10s | right-align in 10 spaces |
%-10s | left-align in 10 spaces |
%,d | thousands separator: 1,000,000 |
A mini program: simple calculator#
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("First number: ");
double a = Double.parseDouble(in.nextLine());
System.out.print("Operator (+ - * /): ");
String op = in.nextLine().trim();
System.out.print("Second number: ");
double b = Double.parseDouble(in.nextLine());
double result = switch (op) {
case "+" -> a + b;
case "-" -> a - b;
case "*" -> a * b;
case "/" -> a / b;
default -> Double.NaN;
};
System.out.printf("%.2f %s %.2f = %.2f%n", a, op, b, result);
}
}
(You’ll learn switch properly in the next lesson.)
Other ways to read input#
BufferedReaderwithInputStreamReader: faster, used in competitive programming.- Command-line
args: values passed when starting the program. - Files: covered in the File I/O lesson.
Common mistakes#
- Forgetting
import java.util.Scanner;. - The
nextInt()thennextLine()empty-line trap. - Creating a new
Scannerinside a loop: create one and reuse it. - Closing a
ScanneronSystem.inand then trying to read again: closing it closes the keyboard stream too.
Exercise#
- Ask for length and width, then print the area and perimeter of a rectangle.
- Ask for a temperature in Celsius and print Fahrenheit with one decimal place.
- Ask for 3 subject marks and print the total, the average (2 decimals) and the grade.
- Print a multiplication table for a number the user enters, nicely aligned with
printf.
Hint for 4
System.out.printf("%2d x %2d = %3d%n", n, i, n * i); inside a loop from 1 to 10.
Summary#
Scannerreads fromSystem.in:nextLine,nextInt,nextDouble,hasNextInt.- Avoid the
nextInt+nextLinetrap by reading lines and parsing them. printfwith%d,%.2f,%s,%-10sand%nmakes output neat.
Next: making decisions with if, else and switch.
Quick quiz
3 questions. Check what stuck and earn 2 XP for each right answer.
Progress is saved in this browser. Create a free account to keep it on every device.
Get new lessons in your inbox
One email when we publish a new tutorial or guide. No spam, unsubscribe in one click.