Reading input with Scanner
18 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Input is what the user types while the program runs, like their name or their marks. Java reads keyboard input with a ready-made tool called Scanner.
Why a program needs input
A greeting program that always prints Hello, Ravi! is wrong for everyone who is not Ravi. To greet whoever runs it, the program must ask for the name while it runs.
The keys the user types go into the standard input, written System.in in Java. A Scanner reads from it into your variable, and the program waits at each read until the user presses Enter.
Four steps to use Scanner
Scanner is in a package (a named group of classes) called java.util. An import line tells Java where to find it.
new Scanner(System.in) creates a Scanner that reads the keyboard. The OOP section explains new; for now, copy the line as it is.
Example 1 · Greet whoever runs it
import java.util.Scanner;
public class Hello { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.println("Hello, " + name + "!"); sc.close(); }}Enter your name: Ravi Hello, Ravi!
What this code does
- Line 1,
import java.util.Scanner;, sits at the very top, above the class. Scanner sc = new Scanner(System.in);creates one Scanner insidemainthat readsSystem.in, the keyboard.- After the prompt,
sc.nextLine()waits for the user, then gives back the whole typed line as a String. sc.close();comes after the last read.
Every Scanner program uses the same four steps: import, create one Scanner, prompt and read, close. Run this one again with another name and the greeting changes with it.
Reading a whole number with nextInt()
You want to tell users how old they will be next year, but nextLine() gives text, and "18" + 1 is "181".
sc.nextInt() reads the typed value as an int, so you can do maths with it.
Example 2 · Age next year
import java.util.Scanner;
public class AgeNextYear { 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("Next year you will be " + (age + 1)); sc.close(); }}Enter your age: 18 Next year you will be 19
What this code does
sc.nextInt()turns the typed18into theint18.(age + 1)is in brackets, so Java adds before joining it to the text.
nextInt() works only for whole numbers. The next lesson (opens in a new tab) shows the methods for decimals, single words and characters.
Prompts, and how a run is shown
At a read the program stops and waits; without a message the user sees an empty screen. A prompt is that message, like Enter your name: .
Print it with print, not println, so the user types on the same line. In a run such as Enter your name: Sneha, the program printed the prompt and the user typed Sneha and pressed Enter.
Trace the code
Watch the program wait for input
Variables
None yet.
Output
(nothing printed yet)Typed input: Sneha ⏎ 88
Line 5A Scanner named sc is ready to read the keyboard. Nothing is printed yet.
Next step
Check your understanding
8 quick questions