Aral Code

Basics Lesson 9 of 21

Java User Input with Scanner

Read keyboard input with java.util.Scanner — nextInt, nextDouble, next and nextLine — avoid the nextLine skipping bug, and build interactive console programs.

Programs become interactive when they ask the user for information. In Java, the standard way to read keyboard input in a console program is the Scanner class.

Setting up a Scanner

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.print("Enter your age: ");
        int age = sc.nextInt();

        System.out.println("Hi " + name + "! Next year you'll be " + (age + 1) + ".");
        sc.close();
    }
}

Output

Enter your name: Maria Clara
Enter your age: 16
Hi Maria Clara! Next year you'll be 17.
  1. import java.util.Scanner; at the very top makes the class available.
  2. new Scanner(System.in) creates a scanner that reads from the keyboard.
  3. print (not println) keeps the cursor on the same line as the question.
  4. sc.close() releases the scanner when you're done.

Scanner methods

MethodReads
nextLine()A whole line of text, including spaces
next()One word (stops at a space)
nextInt()An int
nextDouble()A double
nextBoolean()true or false
next().charAt(0)A single character

A grade calculator

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Written works: ");
        double ww = sc.nextDouble();
        System.out.print("Performance tasks: ");
        double pt = sc.nextDouble();
        System.out.print("Quarterly exam: ");
        double qe = sc.nextDouble();

        double grade = ww * 0.25 + pt * 0.50 + qe * 0.25;
        System.out.printf("Quarterly grade: %.2f%n", grade);
        sc.close();
    }
}

Output

Written works: 88
Performance tasks: 92
Quarterly exam: 85
Quarterly grade: 89.25

The weights (25%, 50%, 25%) follow a typical DepEd component split for TLE and many skills subjects. Your teacher's weights may differ by subject.

The nextLine skipping bug

This program looks fine but skips the name question:

System.out.print("Age: ");
int age = sc.nextInt();
System.out.print("Name: ");
String name = sc.nextLine();   // skipped!

Output

Age: 16
Name:

When you type 16 and press Enter, nextInt() takes the 16 but leaves the Enter (newline) behind. Then nextLine() reads that leftover newline as an empty line and returns immediately.

System.out.print("Age: ");
int age = sc.nextInt();
sc.nextLine();                 // consume the leftover newline
System.out.print("Name: ");
String name = sc.nextLine();   // works now

Wrong input type

If the user types letters when nextInt() expects a number, the program crashes with InputMismatchException. You can check first with hasNextInt():

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("How many pandesal? ");
        while (!sc.hasNextInt()) {
            System.out.print("Numbers only, please: ");
            sc.next();               // throw away the bad input
        }
        int qty = sc.nextInt();
        System.out.println("Total: ₱" + qty * 2.5);
        sc.close();
    }
}

Output

How many pandesal? sampu
Numbers only, please: 10
Total: ₱25.0

One Scanner per program

Create one Scanner at the start of main and reuse it. Creating several scanners on System.in, or closing one and making another, causes confusing errors.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which import is needed for Scanner?
2. Which method reads a full line including spaces?
3. Why does nextLine() sometimes get skipped after nextInt()?
4. What happens if the user types "abc" for nextInt()?

By Sir Reyel · Last updated 2026-12-04