How to Read and Print an Integer Value in Java? (original) (raw)

Last Updated : 09 Apr, 2025

The given task is to take an integer as input from the user and print that integer in Java. To **read and print an integer value in Java, we can use the Scanner class to take input from the user. This class is present in the java.util package.

**Example input/output:

**Input: 357
**Output: 357

**Input: 10
**Output: 10

Using the Scanner Class

The **Scanner class is used to take input from the user through the console. This class provides different methods to take input from users such as integer, String and boolean etc. If we want to take an integer as an input we can use the nextInt() method of the Scanner class. First, we create an object of the Scanner class, then we pass the System.in which is an input stream API in Java helps to interact with the console.

**Import Scanner package:

import java.util.Scanner;

public class Geeks {

public static void main( String [] args){
}

}

Import the Scanner package in the starting either we can import the java.util.* using this we can use all the classes which are present in the util package.

**Syntax of Scanner Class:

Scanner sc = new Scanner(System.in)

int num = sc.nextInt();

**Note: Don’t forget to import the java.util.Scanner package. Alternatively, you can **import java.util.* to access all classes in the util package. Here, **variableOfIntType refers to the variable storing the input value, and **ScannerObject is the previously created Scanner instance.

Printing the Integer Value

We can use the println() or print() method of System class. The System.out.println() method, in Java, prints the value passed as the parameter to it, on the console screen and the changes the cursor to the next line on the console. Whereas **System.out.print() method, in Java, prints the value passed as the parameter to it, on the console screen and the cursor remains on the next character of the last printed character on the console.

**Syntax of println():

System.out.println(num):

The integer value is successfully read and printed.

Complete Java Program to Read and Print Integer Value

Java `

// Java program to take an integer // as input and print it import java.io.*; import java.util.Scanner;

// Driver Class class Geeks { // main function public static void main(String[] args) { // Declare the variables int num;

    // Input the integer
    System.out.println("Enter the integer: ");

    // Create Scanner object
    Scanner s = new Scanner(System.in);

    // Read the next integer from the screen
    num = s.nextInt();

    // Display the integer
    System.out.println("Entered integer is: " + num);

    // Close the Scanner object
    s.close(); // Important to prevent resource leaks
}

}

`

Output:

OutputScreenshot

Similar Reads

Java Basic Programs





















Java Pattern Programs














Java Conversion Programs















Java Classes and Object Programs
















Java Methods Programs











Java Searching Programs




Java 1-D Array Programs













Java 2-D Arrays (Matrix) Programs













Java String Programs