java.util.Scanner

The Scanner class in the java.util package is used to read input (text) from various input sources like:

  • System.in (keyboard)
  • Files
  • Strings
  • Input streams

It provides parsing methods to read different types of data like int, float, double, boolean, String, etc., and is commonly used for interactive user input.

Commonly Used Constructors and Methods

Simple Program

import java.util.Scanner;

public class SimpleScannerExample {
    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();
    }
}

LotusJavaPrince and Mahesh want to develop a student registration system. The system should allow input of multiple student details (name, roll number, branch, marks in 3 subjects). The system should compute average marks and decide if the student is PASS (average ≥ 40) or FAIL.

The program should:

  • Use Scanner for input.
  • Use object-oriented concepts.
  • Display a detailed summary report.
import java.util.Scanner;

class Student {
    private String name;
    private int rollNo;
    private String branch;
    private double[] marks = new double[3];
    private double average;
    private String result;

    public void inputDetails(Scanner scanner) {
        System.out.print("Enter Student Name: ");
        name = scanner.nextLine();

        System.out.print("Enter Roll Number: ");
        rollNo = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        System.out.print("Enter Branch: ");
        branch = scanner.nextLine();

        System.out.println("Enter marks in 3 subjects:");
        double total = 0;
        for (int i = 0; i < 3; i++) {
            System.out.print("Subject " + (i + 1) + ": ");
            marks[i] = scanner.nextDouble();
            total += marks[i];
        }
        scanner.nextLine(); // Consume newline

        average = total / 3;
        result = (average >= 40) ? "PASS" : "FAIL";
    }

    public void displayReport() {
        System.out.println("\n--- Student Report ---");
        System.out.println("Name     : " + name);
        System.out.println("Roll No  : " + rollNo);
        System.out.println("Branch   : " + branch);
        System.out.printf("Average  : %.2f\n", average);
        System.out.println("Result   : " + result);
    }
}

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

        System.out.print("Enter number of students to register: ");
        int count = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        Student[] students = new Student[count];

        for (int i = 0; i < count; i++) {
            System.out.println("\nEnter details for Student " + (i + 1));
            students[i] = new Student();
            students[i].inputDetails(scanner);
        }

        System.out.println("\n=== Summary Report ===");
        for (Student student : students) {
            student.displayReport();
        }

        scanner.close();
    }
}

Scanner simplifies reading input from users or files.

  • Offers various methods like nextInt(), nextLine(), nextDouble() for reading different types.
  • Useful in real-world applications like registration forms, surveys, quizzes, and data entry systems.
Scroll to Top