Java Scanner Input Example

This Java example reads terminal input with Scanner and prints a formatted result. Use it to test beginner class structure, stdin, and System.out output.

Java Compiler

Code Example

import java.util.Scanner;

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

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

        System.out.print("Enter points: ");
        int points = input.nextInt();

        System.out.println(name + " scored " + points + " points.");
    }
}

Expected Output

Enter name: Mayur
Enter points: 42
Mayur scored 42 points.

Practice Steps

  1. Open the Java compiler and paste the code into Main.java.
  2. Run it and answer the terminal prompts.
  3. Add another method to format the final message.
HOME LEARN COMMUNITY DASHBOARD