0
0
JavaHow-ToBeginner · 3 min read

How to Use Scanner in Java: Simple Input Reading Guide

In Java, you use the Scanner class to read input from the user. First, create a Scanner object with new Scanner(System.in), then use methods like nextLine() or nextInt() to get input.
📐

Syntax

The basic syntax to use the Scanner class is:

  • Scanner scanner = new Scanner(System.in); creates a scanner to read from the keyboard.
  • Use methods like nextLine() to read a full line of text.
  • Use nextInt(), nextDouble(), etc., to read specific data types.
  • Always close the scanner with scanner.close(); when done to free resources.
java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int number = scanner.nextInt();
        scanner.close();
    }
}
💻

Example

This example shows how to read a user's name and age from the console and print a greeting message.

java
import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        System.out.print("Enter your age: ");
        int age = scanner.nextInt();
        System.out.println("Hello, " + name + ". You are " + age + " years old.");
        scanner.close();
    }
}
Output
Enter your name: Alice Enter your age: 30 Hello, Alice. You are 30 years old.
⚠️

Common Pitfalls

One common mistake is mixing nextLine() with other next methods like nextInt(). After reading a number, the newline character remains in the input buffer, causing nextLine() to read an empty string.

To fix this, add an extra nextLine() call after reading numbers to consume the leftover newline.

java
import java.util.Scanner;

public class ScannerPitfall {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your age: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // Uncomment this line to fix the issue
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        System.out.println("Name: " + name + ", Age: " + age);
        scanner.close();
    }
}
Output
Enter your age: 25 Enter your name: Name: , Age: 25
📊

Quick Reference

MethodDescription
nextLine()Reads a whole line of text including spaces
next()Reads the next word (up to space)
nextInt()Reads an integer value
nextDouble()Reads a double (decimal) value
close()Closes the scanner and frees resources

Key Takeaways

Create a Scanner with new Scanner(System.in) to read user input.
Use nextLine() for full lines and nextInt(), nextDouble() for numbers.
After nextInt(), call nextLine() to consume leftover newline before reading text.
Always close the Scanner with close() when done.
Scanner helps easily get input from the console in Java programs.