How to Read Integer From Console in Java
- 1). Open your Java Integrated Development Environment (IDE).
- 2). Type the following into a Java file:
System.out.println("Please enter an integer:"); - 3). Type the following on the next line:
Scanner in = new Scanner(System.in);
Normally, if you simply wanted to read ordinary text data from the console, you would read it from "System.in" directly, but here you are using "System.in" to create a Scanner object that allows you to collect data from the console in any of a multitude of formats. - 4). Type the following on the next line to actually read the integer from the user:
int i = in.nextInt();
Source...