In this Java Program, you will learn how to print an Integer entered by the user. The integer is stored in a variable using System.in
, and is displayed on the monitor display using System.out
.
//Java Program to Print an Integer Entered by the User
import java.util.Scanner;
public class PrintInteger {
public static void main(String[] args) {
// Creates a reader instance which takes
// input from standard input - keyboard
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
// nextInt() reads the next integer from the keyboard
int number = reader.nextInt();
// println() prints the following line to the output screen
System.out.println("You entered the Number: " + number);
}
}
Enter a number: 222
You entered the Number: 222
Scanner
class, the reader is created to take inputs from standard input, which is the keyboard.reader.nextInt()
then reads all entered integers from the keyboard unless it encounters a newline character \n
using Enter Key. The entered integers are then saved to the integer variable number.Finally, the number is printed onto the standard output (System.out
) - computer display using the function println()
.
Comments