Home >Java >javaTutorial >How to use nextint in java
nextInt() method is used to read the next integer data (int) from the input stream. 1. Read space character separated integers and convert to int value. 2. Skip all space characters and tab characters in the input stream. 3. If there is no integer available in the input stream or the input data cannot be converted to int type, an InputMismatchException is thrown.
nextInt() method usage in Java
nextInt() method is the Scanner class in the Java programming language Instance method for reading the next integer data (int) from the input stream.
Usage syntax:
<code>int nextInt()</code>
Return value:
nextInt() method returns the next value read from the input stream An integer. If no integer is available in the input stream, an InputMismatchException is thrown.
Detailed description:
Example:
<code class="java">import java.util.Scanner; public class NextIntExample { public static void main(String[] args) { Scanner input = new Scanner(System.in); // 从控制台读取一个整数 int number = input.nextInt(); // 输出读取的整数 System.out.println("读取的整数:" + number); } }</code>
Output:
<code>请输入一个整数: 123 读取的整数:123</code>
The above is the detailed content of How to use nextint in java. For more information, please follow other related articles on the PHP Chinese website!