Home  >  Article  >  Java  >  How to read integers from user input using nextInt() method of Scanner class

How to read integers from user input using nextInt() method of Scanner class

王林
王林Original
2023-07-24 20:31:563174browse

How to use the nextInt() method of the Scanner class to read integers from user input

The Scanner class is a tool class commonly used in Java for reading data from standard input, files or strings . The Scanner class provides various methods to read different types of data, including integers, floating point numbers, strings, etc. Among them, the nextInt() method is the method used to read integers from the input.

The following will introduce in detail how to use the nextInt() method of the Scanner class to read integers from user input, and attach a code example:

Step 1: Introduce the Scanner class
First, The Scanner class needs to be introduced in the code. You can use the following code to complete this operation:

import java.util.Scanner;

Step 2: Create a Scanner object
Before using the Scanner class, you need to create a Scanner object. The Scanner object can be created through the following code, where System.in means reading data from the standard input:

Scanner scanner = new Scanner(System.in);

Step 3: Use the nextInt() method to read the integer
After creating the Scanner object, you can use nextInt () method reads an integer from user input. The nextInt() method will wait for user input until the user enters an integer. The following is a code example that uses the nextInt() method to read an integer:

System.out.print("请输入一个整数:");
int num = scanner.nextInt();
System.out.println("您输入的整数是:" + num);

In the above code, first use the System.out.print() method to output the prompt information, and then call the scanner.nextInt() method to read the integer from the user Read an integer from the input and save it in the variable num. Finally, use the System.out.println() method to print out the read integer.

Step 4: Close the Scanner object
After using the Scanner object, it is recommended to close it manually to release resources. The Scanner object can be closed through the following code:

scanner.close();

The complete sample code is as follows:

import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args) {
        // 创建Scanner对象
        Scanner scanner = new Scanner(System.in);

        // 使用nextInt()方法读取整数
        System.out.print("请输入一个整数:");
        int num = scanner.nextInt();
        System.out.println("您输入的整数是:" + num);

        // 关闭Scanner对象
        scanner.close();
    }
}

Through the above steps and sample code, we can implement the function of reading integers from user input. Of course, the Scanner class also provides many other methods that can be used to read different types of data, such as nextDouble(), nextBoolean(), etc. Depending on the specific needs, we can choose the appropriate method to read the data entered by the user.

I hope this article will help you understand how to use the nextInt() method of the Scanner class to read integers from user input!

The above is the detailed content of How to read integers from user input using nextInt() method of Scanner class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn