How to take Integer Input in Java?

Handling user input is one of the most fundamental concepts in Java programming. Among various data types, integers are widely used for calculations, decision-making, and loops. In this guide, you’ll learn different ways to take integer input in Java, with explanations and best practices.

1. Using Scanner Class (Most Common Method)

The Scanner class in Java is the most widely used way to take integer input. It belongs to the java.util package and provides methods to read various data types, including integers. Learn how to take string input in Java.

Example:

import java.util.Scanner;

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

System.out.print("Enter an integer: ");
int number = scanner.nextInt(); // Reads an integer input

System.out.println("You entered: " + number);

scanner.close(); // Close scanner to prevent memory leaks
}
}

Explanation:

  • Scanner scanner = new Scanner(System.in); → Creates a Scanner object.
  • scanner.nextInt(); → Reads an integer input from the user.
  • scanner.close(); → Prevents memory leaks by closing the scanner.

Handling Input Mismatch Exception

If a user enters a non-integer value, the program may crash. To prevent this, use exception handling:

if(scanner.hasNextInt()) {
    int number = scanner.nextInt();
} else {
    System.out.println("Invalid input! Please enter an integer.");
}

2. Using BufferedReader (Faster for Large Input)

For applications that require reading large inputs efficiently, BufferedReader is a better choice.

Example:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter an integer: ");
int number = Integer.parseInt(reader.readLine());

System.out.println("You entered: " + number);
}
}

Explanation:

  • BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); → Creates a BufferedReader object.
  • reader.readLine(); → Reads input as a string.
  • Integer.parseInt(); → Converts the string input into an integer.
  • Faster than Scanner but requires exception handling.

3. Using Console Class (Recommended for Secure Input)

The Console class is another way to take input, but it works only in command-line interfaces (not IDEs like Eclipse or IntelliJ).

Example:

import java.io.Console;

public class Main {
    public static void main(String[] args) {
        Console console = System.console();
        
        if (console != null) {
            int number = Integer.parseInt(console.readLine("Enter an integer: "));
            System.out.println("You entered: " + number);
        } else {
            System.out.println("Console not available");
        }
    }
}

Explanation:

  • console.readLine(); → Reads input as a string.
  • Integer.parseInt(); → Converts it to an integer.
  • Works only in command-line environments.

4. Using Command-Line Arguments (Alternative Approach)

Another way to provide integer input is through command-line arguments.

Example:

public class Main {
    public static void main(String[] args) {
        if (args.length > 0) {
            int number = Integer.parseInt(args[0]);
            System.out.println("Input received: " + number);
        } else {
            System.out.println("No input provided!");
        }
    }
}

Explanation:

  • The args array stores values passed from the command line.
  • Run the program with: java Main 42 → Output: Input received: 42.

Best Learning Approach: When to Use Each Method?

MethodUse Case
ScannerBest for small programs and user input
BufferedReaderBest for large text input, more performance-efficient
ConsoleBest for secure command-line input
Command-Line ArgsBest for passing predefined values

Summary

Reading integer input in Java is essential for interactive applications. The Scanner class is the easiest, BufferedReader is faster for large input, and Console is ideal for secure input. Choose the right method based on your requirements.

Leave a Comment