Reading Integers from Standard Input in Java
Reading integer values from the standard input (console) is a common task in Java programming. To accomplish this, you can utilize the java.util.Scanner class.
Using Scanner
Instantiate a Scanner object using the new keyword and provide System.in as an argument. System.in represents the standard input stream.
import java.util.Scanner; //... Scanner in = new Scanner(System.in);
Reading an Integer
To read an integer value from the console, use the nextInt() method:
int num = in.nextInt();
The nextInt() method returns the next integer value found in the console input.
Considerations
The above is the detailed content of How to Read Integers from Standard Input in Java?. For more information, please follow other related articles on the PHP Chinese website!