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);
}
}
Enter an integer: 45
The integer you entered is: 45
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
.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
.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.