🌱BeginnerVariables & Data Types

User Input with Scanner

Read keyboard input from users using Java's Scanner class - nextLine, nextInt, nextDouble and more.

Overview

Most programs need to interact with users by accepting input. Java's Scanner class (in the java.util package) makes it easy to read typed input from the keyboard. You create one Scanner instance connected to System.in (standard input) and call its methods to read different types of data. Common Scanner methods: nextLine() reads a whole line as a String, next() reads a single word, nextInt() reads an integer, and nextDouble() reads a decimal number. Always close the Scanner when you are done to free resources.

Key Points

  • •Import Scanner: `import java.util.Scanner;` at the top of the file
  • •Create: `Scanner sc = new Scanner(System.in);`
  • •nextLine() - reads the entire line including spaces, returns String
  • •next() - reads one whitespace-delimited token (word), returns String
  • •nextInt() - reads an integer; throws exception if input is not an integer
  • •nextDouble() - reads a decimal number
  • •Always call sc.close() when done, or use try-with-resources

Examples

Reading a name and printing a greeting

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.println("Hello, " + name + "!");
        sc.close();
    }
}

System.out.print (no ln) keeps the cursor on the same line as the prompt.

Reading different types

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        System.out.print("Enter your GPA: ");
        double gpa = sc.nextDouble();

        sc.nextLine(); // consume leftover newline after nextDouble
        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.printf("%s, age %d, GPA %.2f%n", name, age, gpa);
        sc.close();
    }
}

After nextInt() or nextDouble(), call nextLine() to consume the lingering newline before reading a full line.

Simple calculator using Scanner

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");
        double a = sc.nextDouble();

        System.out.print("Enter second number: ");
        double b = sc.nextDouble();

        System.out.println("Sum: " + (a + b));
        System.out.println("Difference: " + (a - b));
        System.out.println("Product: " + (a * b));
        System.out.printf("Quotient: %.2f%n", a / b);

        sc.close();
    }
}

%.2f in printf formats a double to 2 decimal places.

Common Mistakes

  • Calling nextLine() immediately after nextInt() without a "flush" nextLine() first - the newline from Enter is consumed unexpectedly
  • Forgetting `import java.util.Scanner;` at the top - causes a "cannot find symbol" compile error
  • Creating multiple Scanner instances on System.in - create one and reuse it

Note

The Scanner also works with String input: `Scanner sc = new Scanner("42 3.14");` - useful for testing your parsing code without typing at the keyboard.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

Which Scanner method reads an entire line including spaces?