Java program to print an integer (entered by the user)
www.igift.aedicom
Here's a simple Java program to print an integer entered by the user:
import java.util.Scanner; public class PrintIntegerExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int num = scanner.nextInt(); System.out.println("You entered: " + num); } }
In this program, we first import the java.util.Scanner
class to read user input from the console. We create a new Scanner
object and use the nextInt()
method to read an integer from the user. We then print the entered integer using the println()
method of System.out
.
Output:
Enter an integer: 42 You entered: 42
This program prompts the user to enter an integer, reads the input from the console, and prints the entered integer to the console. If the user enters a non-integer value, the program will throw an InputMismatchException
. You can add error handling code to handle this case if needed.