NextInt() in Java is a method of the Scanner class, used to read integer data from the console. The usage is as follows: Create a Scanner object and initialize it to read data from the console. Call scanner.nextInt() to read the next whitespace-delimited integer. Assign the read integer to a variable.
Usage of nextInt() in Java
##nextInt() is Java
Scanner Built-in method of class, used to read integer data from the console. It reads the next whitespace-delimited integer from the input stream and returns it as an
int value.
Usage:
<code class="java">Scanner scanner = new Scanner(System.in); int number = scanner.nextInt();</code>In the above code:
Create A new
Scanner object that reads input from the console (standard input).
Reads the next integer value from the console and assigns it to variable
number.
Usage example:
<code class="java">// 从控制台读取一个整数 int number = scanner.nextInt(); // 在程序中使用已读入的整数 System.out.println("您输入的数字是:" + number);</code>
Notes:
All leading whitespace characters (spaces, tabs, newlines) will be skipped.
,
nextInt() will throw an exception.
, you must ensure that the input stream has been initialized with a
Scanner object.
object after you are finished using it to free up system resources.
The above is the detailed content of Usage of nextint in java. For more information, please follow other related articles on the PHP Chinese website!