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:
import java.util.Scanner;
Scanner object:
Scanner scanner = new Scanner(System.in);
nextInt Function reads integer:
int number = scanner.nextInt();
##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: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>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);
}
}</pre><div class="contentsignin">Copy after login</div></div>
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!