Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 8 of 10
User Input with Scanner
Scanner reads user input from the console. In real applications input comes from APIs or UI, but Scanner is how you build interactive command-line Java programs and understand the fundamentals of input parsing.
Scanner - Reading Input Safely
import java.util.Scanner;
import java.util.InputMismatchException;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // reads full line including spaces
System.out.print("Enter your age: ");
int age = scanner.nextInt(); // reads integer only
scanner.nextLine(); // consume leftover newline!
System.out.print("Enter price: ");
double price = scanner.nextDouble();
System.out.printf("Hello %s (age %d), price = %.2f%n", name, age, price);
// Defensive input reading with validation
System.out.print("Enter a positive number: ");
try {
int num = scanner.nextInt();
if (num <= 0) throw new IllegalArgumentException("Must be positive");
System.out.println("You entered: " + num);
} catch (InputMismatchException e) {
System.out.println("Error: Please enter a valid integer.");
} finally {
scanner.close(); // always close Scanner when done
}
}
}Common Scanner Pitfalls
- After nextInt() or nextDouble(), call nextLine() to consume the leftover newline - otherwise the next nextLine() reads an empty string.
- nextLine() reads the ENTIRE line; next() reads a single token (word).
- Always validate input - InputMismatchException is thrown for wrong types.
- Use try-with-resources: try (Scanner sc = new Scanner(System.in)) { } to auto-close.
Quick Quiz
Tip
Tip
Practice User Input with Scanner in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for User Input with Scanner
Practice Task
Note
Practice Task - (1) Write a working example of User Input with Scanner from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with User Input with Scanner is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- Scanner reads user input from the console.
- After nextInt() or nextDouble(), call nextLine() to consume the leftover newline - otherwise the next nextLine() reads an empty string.
- nextLine() reads the ENTIRE line; next() reads a single token (word).
- Always validate input - InputMismatchException is thrown for wrong types.
Finished this lesson?