User Input with Scanner
Until now your programs only printed hard-coded values. Real programs react to people. The Scanner class reads what the user types at the keyboard — turning your code into an interactive conversation.
Learn User Input with Scanner in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Reading keyboard input always follows the same recipe:
💡 What is System.in? Just as System.out is the output stream (the screen), System.in is the input stream (the keyboard). A Scanner wraps that raw stream and gives you friendly methods to pull out words, lines, and numbers.
Tip: use System.out.print (no ln ) for prompts so the cursor stays on the same line as the user's typing.
The Scanner has a method for each kind of value. Pick the one that matches the data you expect:
Almost every beginner hits this bug. When you call nextInt() and the user types 30 and presses Enter, the Scanner reads 30 but leaves the invisible newline character (from Enter) sitting in the buffer.
The next nextLine() then immediately reads that leftover newline and returns an empty string — it looks like your prompt was skipped!
If you call nextInt() and the user types "banana" , the program crashes with an InputMismatchException . Professional code never assumes the input is valid.
The hasNextInt() method peeks at the next token without consuming it, returning true only if it could be read as an int. Loop on it to keep asking until the input is good:
Put these lines in the right order to ask for a name and print a greeting.
Why: You must create the Scanner before using it. Print the prompt, then read the answer into name , then use name in the greeting. Closing the Scanner comes last, once you no longer need input.
1. The user types Mary Jane and presses Enter. What does scanner.next() return?
"Mary" . next() stops at the first space, reading only one token. nextLine() would return the whole "Mary Jane" .
2. After int age = scanner.nextInt(); with the user typing 40 + Enter, what does the very next scanner.nextLine() return?
An empty string "" — it reads the leftover newline. That's the gotcha; add an extra nextLine() to flush it.
3. The user types abc when the code calls scanner.nextInt() . What happens?
An InputMismatchException is thrown and the program crashes — unless you guarded it with hasNextInt() first.
Your programs can now talk to people. You can import and create a Scanner , read lines, words, ints and doubles, dodge the leftover-newline trap, and validate input with hasNextInt() so a typo never crashes your program.
Next up: The Math Class — square roots, rounding, powers, random numbers and more, ready to apply to the input you just collected.
Practice quiz
Why must you import Scanner but not String or System?
- Scanner is a third-party library
- String and System are keywords
- Scanner lives in java.util, which is not auto-imported; String and System are in java.lang which is
- Scanner does not need importing either
Answer: Scanner lives in java.util, which is not auto-imported; String and System are in java.lang which is. Core classes in java.lang are imported automatically. Scanner is in java.util, so you must add import java.util.Scanner; or the compiler can't find the symbol.
The user types "Mary Jane" and presses Enter. What does scanner.next() return?
- "Mary"
- "Mary Jane"
- "Jane"
- An empty string
Answer: "Mary". next() reads a single whitespace-delimited token, stopping at the first space, so it returns "Mary". nextLine() would return the whole line.
What is the difference between next() and nextLine()?
- next() reads a whole line; nextLine() reads one word
- They are identical
- next() reads numbers only
- next() reads one whitespace-delimited token; nextLine() reads the rest of the line including spaces
Answer: next() reads one whitespace-delimited token; nextLine() reads the rest of the line including spaces. next() reads one word and stops at whitespace; nextLine() reads everything up to the newline. Use nextLine() for full names or addresses.
After int age = scanner.nextInt(); with the user typing 40 + Enter, what does the very next scanner.nextLine() return?
- "40"
- An empty string
- The next line of input
- null
Answer: An empty string. nextInt() leaves the trailing newline in the buffer, so the next nextLine() reads that leftover newline and returns an empty string immediately - the famous gotcha.
What happens if the user types text when your code calls scanner.nextInt()?
- It throws an InputMismatchException and the program crashes
- It returns 0
- It silently skips the input
- It returns the text as a String
Answer: It throws an InputMismatchException and the program crashes. nextInt() on non-numeric input throws InputMismatchException. Guard it with hasNextInt() first and discard bad tokens with next().
How do you fix the skipped-nextLine() newline bug?
- Use two Scanners
- Avoid nextInt() entirely
- Add an extra scanner.nextLine() after nextInt()/nextDouble() to swallow the leftover newline
- Call scanner.close() between reads
Answer: Add an extra scanner.nextLine() after nextInt()/nextDouble() to swallow the leftover newline. After a nextInt() or nextDouble() that is followed by a nextLine(), add one extra scanner.nextLine() to consume the dangling newline.
What does scanner.hasNextInt() do?
- Reads and returns the next int
- Peeks at the next token without consuming it, returning true only if it could be read as an int
- Throws if the next token is not an int
- Closes the scanner
Answer: Peeks at the next token without consuming it, returning true only if it could be read as an int. hasNextInt() peeks without consuming, returning true only if the next token is a valid int. Loop on it to validate input before calling nextInt().
Which method reads a decimal number like 1.82 from the keyboard?
- nextInt()
- nextLine()
- next()
- nextDouble()
Answer: nextDouble(). nextDouble() reads and returns a double. nextInt() reads a whole number; nextLine()/next() read text.
Why use System.out.print (no ln) for a prompt before reading input?
- It is required by Scanner
- So the cursor stays on the same line as the user's typing
- println does not work with Scanner
- It reads input automatically
Answer: So the cursor stays on the same line as the user's typing. print keeps the cursor on the same line, so the prompt and the user's typed answer sit together, which reads more naturally.
What is true about scanner.close() on a Scanner wrapping System.in?
- It is forbidden
- It reopens System.in
- Closing it also closes System.in, so don't create another Scanner on System.in afterward
- It must be called before every read
Answer: Closing it also closes System.in, so don't create another Scanner on System.in afterward. Closing a Scanner on System.in also closes System.in, so a second Scanner created afterward will fail. Close it once when you're done reading.