Java Program To Print Integer Using User Input
Chapter:
Java Basics
Last Updated:
25-08-2017 08:02:33 UTC
Program:
/* ............... START ............... */
import java.util.Scanner;
public class JavaPrintInteger {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = reader.nextInt();
System.out.println("You entered: " + number);
}
}
/* ............... END ............... */
Output
Enter a number: 5
You entered: 5
Notes:
-
Scanner class reader is used to created to take inputs from standard input, which is keyboard.
- reader.nextInt() reads all entered integers from the keyboard unless it encounters a new line character \n (Enter).
- If you enter any character which is not an integer, the compiler will throw an InputMismatchException.
Tags
Print Integer Using User Input, Java, Basics