How to Read and Print an Integer Value in Java

Last Updated : 12 May, 2026

Reading and printing an integer value in Java is a fundamental concept used to handle user input and display output. It allows programs to interact with users by accepting numeric data and showing results. Java provides built-in support through classes like Scanner and output methods such as System.out.println(), making input-output operations simple and efficient.

  • It helps in building interactive programs by taking user input at runtime
  • It forms the base for performing calculations, conditions, and logical operations in Java programs

Ways to Read and Print an Integer in Java

It explains different methods like Scanner and BufferedReader to take integer input from the user and display it on the screen.

1. Using Scanner Class

The Scanner class in java.util package is the easiest way to take input from the user in Java. It reads data from various input sources, including the keyboard.

Import Declaration:

import java.util.Scanner

Steps to Read and Print an Integer using a Scanner

  • Import the Scanner class.
  • Create a Scanner object to read input.
  • Prompt the user for input.
  • Read the integer using nextInt().
  • Print the integer using System.out.println().
Java
import java.util.Scanner;

public class GFG{
    
    public static void main(String[] args){
        
        // Step 1: Create Scanner object
        Scanner sc = new Scanner(System.in);

        // Step 2: Prompt user for input
        System.out.print("Enter an integer: ");

        // Step 3: Read integer input
        int number = sc.nextInt();

        // Step 4: Print the entered integer
        System.out.println("You entered: " + number);

        // Close the scanner
        sc.close();
    }
}

Output:

output
output

Explanation:

  • Scanner sc = new Scanner(System.in); -> Creates a Scanner object to take input.
  • nextInt() -> Reads an integer value entered by the user.
  • System.out.println() -> Prints the entered integer on the screen.

2. Using BufferedReader

The BufferedReader class provides a faster way to read input, especially for large-scale programs or competitive coding. However, it reads input as a string, so you must convert it into an integer using Integer.parseInt().

Steps to Read and Print an Integer Using BufferedReader

  • Import the required classes from java.io.
  • Create a BufferedReader object.
  • Prompt the user for input.
  • Read the input using readLine().
  • Convert the string input to an integer using Integer.parseInt().
  • Display the value using System.out.println().
Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GFG {

    public static void main(String[] args)
        throws IOException{
            
        // Step 1: Create BufferedReader object
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(System.in));

        // Step 2: Prompt user for input
        System.out.print("Enter an integer: ");

        // Step 3: Read and parse input
        int number = Integer.parseInt(reader.readLine());

        // Step 4: Print the entered integer
        System.out.println("You entered: " + number);
    }
}

Output:

output
output

Explanation:

  • BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); -> Reads input as text.
  • Integer.parseInt(reader.readLine()) -> Converts the string input into an integer.
  • System.out.println() -> Displays the integer value entered by the user.
Comment