Home > Java > javaTutorial > body text

Use java's Scanner.hasNextInt() function to determine whether the next input is an integer

王林
Release: 2023-07-25 13:13:09
Original
1499 people have browsed it

Code example of using Scanner to determine whether the next input is an integer

Scanner is a very commonly used class in Java, which can be used to obtain user input. For determining whether the next input is an integer, Scanner provides a very convenient function hasNextInt(). The hasNextInt() function returns a Boolean value. If the next input is an integer, it returns true, otherwise it returns false.

The following is a simple sample code that demonstrates how to use Scanner's hasNextInt() function to determine whether the next input is an integer:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("请输入一个整数:");
        if (scanner.hasNextInt()) {
            int num = scanner.nextInt();
            System.out.println("您输入的整数是:" + num);
        } else {
            System.out.println("您输入的不是一个整数!");
        }
        
        scanner.close();
    }
}
Copy after login

In the above code, we first create A Scanner object and pass System.in as a parameter to the Scanner constructor to get the user's input. Then we use the hasNextInt() function to determine whether the next input is an integer. If the return value is true, it means that the next input is an integer, and we can get the value of this integer by calling the nextInt() function. If the return value is false, it means that the next input is not an integer.

In the sample code, we first print the prompt message and ask the user to enter an integer. Then we use the hasNextInt() function to determine whether the user's input is an integer. If it is an integer, we store it in the variable num and print it out; if it is not an integer, we print out the corresponding prompt information.

It should be noted that in order to prevent resource leaks, we close the Scanner object by calling the scanner.close() function at the end of the code.

Through the above example, we can easily determine whether the next input is an integer and handle different situations accordingly. This can increase the robustness of the program and prevent the program from exceptions due to user input errors. If you need to check whether the user's input is an integer multiple times, you can use a loop to make full use of the convenience of the hasNextInt() function.

The above is the detailed content of Use java's Scanner.hasNextInt() function to determine whether the next input is an integer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template