Home >Java >javaTutorial >The role of nextint function
nextInt function reads an integer (int type), using steps: 1. Import the java.util.Scanner package; 2. Create a Scanner object; 3. Call the nextInt function to read the integer.
The role of nextint function
nextInt
The function is Scanner# in Java ## A method of class used to read an integer (
int type).
How to use the nextInt function
nextInt function, follow these steps:
java.util.Scanner Package:
<code class="java">import java.util.Scanner;</code>
Scanner object:
<code class="java">Scanner scanner = new Scanner(System.in);</code>
nextInt Function reads integer:
<code class="java">int number = scanner.nextInt();</code>
##nextInt Detailed description of the function
It will skip any leading spaces or tabs. InputMismatchException
exception.
The following code example shows how to read an integer using the
nextInt function: <pre class="brush:php;toolbar:false"><code class="java">import java.util.Scanner;
public class NextIntExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an integer: ");
int number = scanner.nextInt();
System.out.println("You entered: " + number);
}
}</code></pre>
Output:
<code>Enter an integer: 123 You entered: 123</code>
The above is the detailed content of The role of nextint function. For more information, please follow other related articles on the PHP Chinese website!