Home Java Java User Input

Java User Input

Beginner ⏱ 6 min read Updated: Jun 2026

Java user input allows users to provide data to a program while it is running. In Java, we can take input from the user using the Scanner class available in the java.util package.

Taking user input makes Java programs interactive because users can enter values like numbers, names, and other information according to their requirements.

What is User Input in Java?

User input in Java means receiving data from the user through the keyboard. The program waits for the user to enter a value and then uses that value to perform different operations.

Java provides the Scanner class to read different types of input such as strings, integers, floating-point numbers, and boolean values.

💡
Key Point: The Scanner class is the most common way to get user input in Java programs.

Import Scanner Class in Java

Before using the Scanner class, we need to import it from the java.util package.

Syntax
import java.util.Scanner;

Creating Scanner Object

After importing the Scanner class, we need to create an object of Scanner to read input from the user.

Example

Scanner  input = new  Scanner(System.in);

Java User Input Example

The following example shows how to take a user's name as input and display it.

Example

import 
java.util.Scanner;
public class Main{
    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);
    }
    }
Output
Enter your name: Shivam
Hello Shivam

Reading Different Types of Input in Java

The Scanner class provides different methods to read various types of data from users.

nextInt()

Used to read integer values from the user.

nextDouble()

Used to read decimal numbers.

nextLine()

Used to read complete text or sentences.

nextBoolean()

Used to read true or false values.

Example of Taking Integer Input

Example

Scanner input = new Scanner(System.in); 
System.out.print("Enter age: ");
int age = input.nextInt(); 
System.out.println(age);
Output
Enter age: 20
20

Why Use Scanner Class in Java?

  • It allows programs to interact with users.
  • It supports different data types.
  • It is simple and beginner-friendly.
  • It is included in Java's standard library.

Java User Input Best Practices

Close Scanner

Always close the Scanner object after completing input operations.

Validate Input

Check user input before processing data to avoid errors.

Correct Method

Use the correct Scanner method according to the required data type.