Learn Java
import com.learnjava.ioformatting.ScannerInput;

Taking Input with Scanner in Java

@Concept

Scanner, from java.util, is the standard beginner-friendly way to read input in Java. Creating one wraps System.in — the program's standard input stream — and its methods like nextInt(), nextDouble(), and nextLine() each read and parse the next token as a specific type, blocking until the user actually types something and presses enter.

Mixing nextInt() or nextDouble() with nextLine() is the single most common Scanner gotcha. The numeric methods only consume the number itself, leaving the trailing newline in the buffer — so a nextLine() called right after can immediately (and unexpectedly) return an empty string, unless that leftover newline is explicitly consumed first.

@HowItWorks
1

Create a Scanner

new Scanner(System.in) wraps standard input so its methods can read whatever the user types.

2

Read a specific type

nextInt(), nextDouble(), and nextBoolean() each read the next token and parse it as that type, throwing an exception if it doesn't match.

3

Read a full line

nextLine() reads everything up to the next newline, including spaces, unlike the type-specific methods which stop at whitespace.

4

The newline gotcha

nextInt() leaves the newline after the number in the buffer, so a nextLine() called right after can return an unexpectedly empty string.

5

Close it when done

Calling close() releases the underlying resource, which matters more for file or network sources than for System.in, but is still good practice.

@Example
import java.util.Scanner;
 
public class InputDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("You are " + age + " years old.");
}
}
Output
Enter your age: 30
You are 30 years old.
1System.out.print with no newline keeps the cursor on the same line, so whatever the user types next appears to continue that line.
2scanner.nextInt() blocks until the user types something and presses enter, then parses it as an int.
3The terminal itself echoes the typed 30, which is why it appears right after the prompt on the same line shown above.
4println then starts a fresh line for the confirmation message.
@UseCases
Building a simple command-line tool that asks the user a question and reacts to the answer
Reading a series of numbers or words typed one at a time during a program's run
Debugging an unexpectedly empty string returned by nextLine() called right after nextInt()
Prototyping program logic quickly without needing a GUI or a file to read from

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.