简体   繁体   English

如何检查int是否包含字母

[英]How to check if an int contains a letter

I am trying to validate my code by error checking. 我正在尝试通过错误检查来验证我的代码。 I want to make sure the integer people enter does not contain a letter or more. 我想确保输入的整数不包含字母或更多。 Here is my code. 这是我的代码。 I am supposed to solve this problem using a one dimensional array. 我应该使用一维数组解决此问题。 I got the code working but I am having problems with adding the error checking in. Any help would be appreciated. 我可以使用代码,但是在添加错误检查时遇到了问题。我们将不胜感激。 Thanks 谢谢

public void getNumbers() {
    Scanner keyboard = new Scanner(System.in);

    int array[] = new int[5];
    int count = 0;
    int entered = 0;
    int k = -1;

    while (entered < array.length) {

        System.out.print("Enter a number ");
        int number = keyboard.nextInt();

        if (10 <= number && number <= 100) {

            boolean containsNumber = false;

            entered++;

            for (int i = 0; i < count; i++) {
                if (number == array[i]) // i Or j
                {
                    containsNumber = true;
                }
            }
            if (!containsNumber) {
                array[count] = number;
                count++;
            } else {
                System.out.println(number + " has already been entered");
            }
        } else {
            System.out.println("number must be between 10 and 100");
        }

        //what does %d do?
        for (int j = 0; j < count; j++) {
            System.out.printf("%d ", array[j]);
        }
        System.out.println();

    }
}

} }

I'm assuming that you would want your program to ask the user to re-enter a number if they do not input a number the first time. 我假设您希望程序在用户第一次不输入数字时要求用户重新输入数字。 In this scenario you might want to try something along the lines of this: 在这种情况下,您可能需要尝试以下方法:

Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
while(!sc.hasNextInt()) {
    //print some error statement
    sc.nextLine(); 
}
int number = sc.nextInt();
System.out.println("Number is: " + number); // to show the value of number
// continue using number however you wish

Since hasNextInt() returns a boolean determining whether or not the input is an Integer, the program will never leave the while-loop until the program can confirm that the user has entered an integer. 由于hasNextInt()返回一个布尔值,确定输入是否为整数,所以程序将永远不会离开while循环,直到程序可以确认用户输入了整数为止。

keyboard.nextInt() will throw a InputMismatchException if you input a String. 如果您输入String,则keyboard.nextInt()将引发InputMismatchException

If you want to check whether Scanner has an integer to read, you can use keyboard.hasNextInt() . 如果要检查Scanner是否具有要读取的整数,则可以使用keyboard.hasNextInt()

Alternatively, you can read the input as String s = keyboard.next() which will take the input as a String, and then use s.matches(".*\\\\d+.*") to detect whether or not it is an integer. 或者,您可以将输入读取为String s = keyboard.next() ,它将输入作为字符串,然后使用s.matches(".*\\\\d+.*")来确定它是否为整数。

UPDATE: To answer questions - keyboard.hasNextInt() will return a boolean. 更新:要回答问题keyboard.hasNextInt()将返回一个布尔值。 So for example, after System.out.print("Enter a number") , you could have an if statement checking to see if keyboard can receive numerical input, ie. 因此,例如,在System.out.print("Enter a number") ,您可以使用if语句检查keyboard可以接收数字输入,即。 if(keyboard.hasNextInt) . if(keyboard.hasNextInt) If this is true, that means the user has entered numerical input, and you could continue with saying int number = keyboard.nextInt() . 如果为true,则表示用户已输入数字输入,您可以继续说int number = keyboard.nextInt() If it is false, you would know that the user input is non-numerical. 如果为假,则您将知道用户输入是非数字的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查 char[] 是否只包含一个字母和一个 int - check if char[] contains only one letter and one int 检查 int[] 是否包含一个 int - Check if int[] contains an int 如何检查 int 是否包含特定数字? - How to check if an int contains specific numbers? 如何使用正则表达式检查密码中是否包含字母和数字 - how to use regex to check the password contains letter and number 如何检查字符串是否包含与Java中给定范围不同的字母 - How to check if a string contains a letter different then a given range in Java 如何检查单词是否包含一个字母或一组字母? - How do I check to see if a word contains a letter or group of letters? 如何检查字符串是否包含小写字母,大写字母,特殊字符和数字? - How to check whether a string contains lowercase letter, uppercase letter, special character and digit? 如何检查 Java 字符串是否至少包含一个大写字母、小写字母和数字? - How do I check if a Java String contains at least one capital letter, lowercase letter, and number? 如何检查JOptionPane输入是否包含带有2个if语句的int或double并转换? - How to check if JOptionPane input contains an int or double with 2 if statements and is converted? 如何检查文本是否仅包含 Java 中的 int 值? - How to check if a text contains only int values in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM