Home >Java >javaTutorial >How to Read Integer Values from Standard Input in Java?
Reading Integer Values from Standard Input in Java
In Java, there are several classes that can be used for reading input from the standard input stream. However, the most commonly used class for this task is java.util.Scanner.
Using java.util.Scanner
To read an integer value from the standard input using Scanner, follow these steps:
import java.util.Scanner;
Scanner in = new Scanner(System.in);
int num = in.nextInt();
Example
Here's an example of how to use Scanner to read an integer value from the standard input:
import java.util.Scanner; public class Example { public static void main(String[] args) { // Create a Scanner object Scanner in = new Scanner(System.in); // Read an integer value int num = in.nextInt(); // Print the integer value System.out.println(num); } }
This code reads an integer value from the standard input and prints it to the console.
The above is the detailed content of How to Read Integer Values from Standard Input in Java?. For more information, please follow other related articles on the PHP Chinese website!