VisualStudioTutor.com Java Tutorial All Lessons

Lesson 7 of 20

Input and Output

Display information and read keyboard input using Scanner, while avoiding common input-buffer problems.

Learning objectives

  • Create a Scanner connected to keyboard input.
  • Read text and numeric values safely.
  • Format console output with printf.
  • Explain the difference between nextLine() and numeric Scanner methods.
  • Build a small interactive program.

1. Reading keyboard input

Scanner is a standard Java class for reading input. For console programs, it is commonly connected to System.in.

Example: read a name

import java.util.Scanner;

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

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

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

nextLine() reads a complete line, including spaces between words.

2. Reading numbers

Example: read price and quantity

import java.util.Scanner;

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

        System.out.print("Unit price: ");
        double price = input.nextDouble();

        System.out.print("Quantity: ");
        int quantity = input.nextInt();

        double total = price * quantity;
        System.out.printf("Total: RM %.2f%n", total);

        input.close();
    }
}

3. The nextInt/nextLine trap

After nextInt() or nextDouble(), the newline entered by the user remains in the input buffer. A following nextLine() may read that leftover newline immediately.

Example: consume the leftover newline

System.out.print("Age: ");
int age = input.nextInt();
input.nextLine(); // consume the pending newline

System.out.print("Full name: ");
String name = input.nextLine();

4. A robust beginner alternative

Another approach is to read everything with nextLine() and convert text when you need numbers.

System.out.print("Enter quantity: ");
int quantity = Integer.parseInt(input.nextLine());

System.out.print("Enter price: ");
double price = Double.parseDouble(input.nextLine());

Mini project: simple receipt

import java.util.Scanner;

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

        System.out.print("Product name: ");
        String product = input.nextLine();

        System.out.print("Unit price: RM ");
        double price = Double.parseDouble(input.nextLine());

        System.out.print("Quantity: ");
        int quantity = Integer.parseInt(input.nextLine());

        double total = price * quantity;

        System.out.println("
--- Receipt ---");
        System.out.println("Product : " + product);
        System.out.println("Quantity: " + quantity);
        System.out.printf("Total   : RM %.2f%n", total);

        input.close();
    }
}

Common mistakes

ProblemWhy it happensHow to fix it
Name input is skippedA leftover newline was read by nextLine().Consume the newline or use nextLine() consistently.
NumberFormatExceptionText that is not numeric was converted with parseInt or parseDouble.Validate input or handle the exception in a later lesson.
Scanner imported incorrectlyThe class is in java.util.Add import java.util.Scanner;.

Summary

You can now build interactive console programs that accept user input and display formatted output. Lesson 8 introduces operators so you can perform richer calculations and comparisons with those values.