Read User Input in Java with Scanner (and the Leftover Enter Trap)
Scanner on System.in turns what the user types into Java values. Five small programs: read cups with nextInt, a full name with nextLine, the Enter that nextInt leaves behind, a coffee receipt, and every input type side by side.
What you will learn
- Create a Scanner on System.in and read a number with nextInt
- Pick the right method: nextInt, nextDouble, nextBoolean, next or nextLine
- Fix the empty-string bug when nextLine follows nextInt
- Recognise an InputMismatchException when the answer doesn't match the method
Before you start
- Java strings and text blocks
- Java methods and overloading
Until now every value was typed straight into the code, so a program always gave the same output. Think of an ATM that couldn't ask how much you want: it could only ever hand out one fixed amount. Scanner lets your program ask the user instead. It asks a question, waits while the cursor blinks, and moves on only when the user types an answer and presses Enter.
Three lines and the program listens
package org.studyeasy.s02l15;
import java.util.Scanner;
public class Ex01_ReadOneNumber {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("How many cups? ");
int cups = keyboard.nextInt();
System.out.println("Brewing " + cups + " cups.");
keyboard.close();
}
}
How many cups? 3
Brewing 3 cups.
(The 3 is what the user typed; IntelliJ shows typed text in green in the Run window.)
Scannerlives injava.util, so you import it. IntelliJ adds the import for you.new Scanner(System.in)creates a Scanner calledkeyboard.System.inis the keyboard input.print, notprintln, so the cursor stays on the question's line.keyboard.nextInt()is where the program waits for a number and Enter.
One whole line, spaces included
package org.studyeasy.s02l15;
import java.util.Scanner;
public class Ex02_ReadAWholeLine {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// print, not println: the cursor waits on the same line as the question.
System.out.print("Your full name: ");
// nextLine() reads everything up to Enter, spaces included.
String name = keyboard.nextLine();
System.out.println("Welcome, " + name + "!");
keyboard.close();
}
}
Your full name: Mira Nair
Welcome, Mira Nair!
A name can have two words, so nextLine is the right choice. Swap it for next() and "Mira Nair" arrives as just "Mira".
Pick the method that matches the answer
| Method | Reads | User types | Java gets |
|---|---|---|---|
nextInt() | a whole number | 25 | 25 |
nextDouble() | a decimal (a whole number works too) | 18.5 | 18.5 |
nextBoolean() | only true or false | true | true |
next() | one word | Mira Nair | "Mira" |
nextLine() | the whole line | Mira Nair | "Mira Nair" |
nextInt and nextLine are the two you'll use most.
nextInt() leaves the Enter behind
package org.studyeasy.s02l15;
import java.util.Scanner;
public class Ex03_TableAndName {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Table number: ");
int table = keyboard.nextInt();
// nextInt() leaves the Enter key behind, so read and drop it here.
keyboard.nextLine();
System.out.print("Customer name: ");
String name = keyboard.nextLine();
System.out.println("Table " + table + " -> " + name);
keyboard.close();
}
}
Table number: 7
Customer name: Anita Rao
Table 7 -> Anita Rao
The user typed 7, Enter, Anita Rao, Enter. nextInt takes the 7 and stops, but the first Enter is still waiting. The bare keyboard.nextLine() reads that Enter and drops it. Then the real nextLine gets "Anita Rao".
Delete the bare line and the name question reads the leftover Enter instead: it returns an empty String immediately and never stops for the name. This is the most common Scanner bug.
A receipt from three answers
package org.studyeasy.s02l15;
import java.util.Scanner;
public class Ex04_CoffeeReceipt {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// The line goes first, so there is no leftover Enter to clean up.
System.out.print("Customer name: ");
String name = keyboard.nextLine();
System.out.print("Cups: ");
int cups = keyboard.nextInt();
// The price can have decimals, so it is a double.
System.out.print("Price per cup: ");
double price = keyboard.nextDouble();
System.out.println(name + " ordered " + cups + " cups");
System.out.println("Total: " + (cups * price));
keyboard.close();
}
}
Customer name: Anita Rao
Cups: 3
Price per cup: 180.5
Anita Rao ordered 3 cups
Total: 541.5
Asking for the name first avoids the trap: nothing was read before it. The answers are real numbers now, so 3 x 180.5 = 541.5. Move the name question to the end, after nextDouble, and the receipt has no name.
All the types in one program
package org.studyeasy.s02l15;
import java.util.Scanner;
public class Ex05_InputTypes {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Age: ");
int age = keyboard.nextInt();
System.out.print("Price: ");
double price = keyboard.nextDouble();
System.out.print("Member: ");
boolean member = keyboard.nextBoolean();
// next() takes one word and leaves the rest of the line behind.
System.out.print("One word: ");
String word = keyboard.next();
keyboard.nextLine();
System.out.print("Full line: ");
String line = keyboard.nextLine();
System.out.println("Age " + age + ", price " + price + ", member " + member);
System.out.println("next() gave " + word);
System.out.println("nextLine() gave " + line);
keyboard.close();
}
}
Age: 25
Price: 18.5
Member: true
One word: Mira Nair
Full line: Anita Rao
Age 25, price 18.5, member true
next() gave Mira
nextLine() gave Anita Rao
next() took only "Mira"; the rest of that line was still waiting, so a bare nextLine() drops it before the last question.
The answer must match the method
Type hello where nextInt expects a number and the program stops with an InputMismatchException. Scanner won't guess. nextInt wants a whole number, nextDouble a number, nextBoolean true or false.
Recap
new Scanner(System.in)reads the keyboard;import java.util.Scanner.- Ask with
print, read with the method that matches the answer. - After
nextInt,nextDoubleornext, clear the leftover Enter with a barenextLine()before reading a line. - A mismatched answer throws
InputMismatchException.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
