Google News
logo
Java Program to Print an Integer (Entered by the User)
In the following example of Java program to print an integer entered by the user :
Program :
import java.util.Scanner;

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

        System.out.print("Enter an integer: ");
        int num = input.nextInt();

        System.out.println("The integer you entered is: " + num);
    }
}
Output :
Enter an integer: 45
The integer you entered is: 45
* This program first imports the Scanner class from the java.util package, which allows us to read input from the user. Then it creates a new Scanner object called input.

* The program prompts the user to enter an integer using the System.out.print() method, and then reads the integer using the nextInt() method of the Scanner class. The entered integer is stored in the variable num.

* Finally, the program uses the System.out.println() method to print out the entered integer. The program concatenates the string "The integer you entered is: " with the value of num using the + operator. This results in the entered integer being printed to the console.