如何验证字符串只使用数字并输出一条消息,说明它必须只是Bluej上的数字

时间:2022-02-12 05:28:21

I need to make the code below validate a string to only use numbers. I did an If statement but any time I run the program it outputs a message saying that the string cannot be integers.

我需要使下面的代码验证字符串只使用数字。我做了一个If语句但是每次运行程序时它都会输出一条消息,说明字符串不能是整数。

Here are the variables:

以下是变量:

private String personalIdNo;

private boolean isValidPersonalIdNo; 

Here is the code I am having problems with, it won't work it always outputs "not integer" even if I put it as all numbers.

这是我遇到问题的代码,即使我把它作为所有数字,它总是会输出“not integer”。

public void personalIdValidator()
{

    if (personalIdNo.matches("[0-9]"))
    {
        //Return True
    }
    else{
        isValidPersonalIdNo = false;
        System.out.println("The input is not only int");
    }

}

1 个解决方案

#1


2  

Your check is almost correct, you just need to replace personalIdNo.matches("[0-9]") with either personalIdNo.matches("[0-9]+") or personalIdNo.matches("[0-9]*"), depending on whether you want the empty string to be valid as well.

您的检查几乎是正确的,您只需要将personalIdNo.matches(“[0-9]”)替换为personalIdNo.matches(“[0-9] +”)或personalIdNo.matches(“[0-9] * “),取决于您是否希望空字符串也有效。

Explanation:

Your current pattern means "one single character between 0 and 9". Literally "one single character", so e.g. "1" or "4" would be valid inputs even with your code. To allow for more than one character, there are two options:

您当前的模式意味着“0到9之间的单个字符”。字面意思是“单个字符”,例如即使您的代码,“1”或“4”也是有效的输入。要允许多个字符,有两个选项:

  1. + means "one or more characters"
  2. +表示“一个或多个字符”

  3. * means "zero or more characters" (i.e. allowing the empty string as well)
  4. *表示“零个或多个字符”(即允许空字符串)

  5. (technically you could also specify a concrete number with {min,max}, but I don't think that's what you want)
  6. (从技术上讲,你也可以用{min,max}指定一个具体数字,但我认为这不是你想要的)

Note that this pattern currently also prevents negative numbers. If you want to also allow negative numbers, you could use personalIdNo.matches("-?[0-9]+"), where ? means "zero or one occurrences".

请注意,此模式目前也可以防止负数。如果你还想允许负数,你可以使用personalIdNo.matches(“ - ?[0-9] +”),在哪里?表示“零或一次出现”。

Proof/Code sample:

public class NumberTest {

    public static void main(String[] args) {
        personalIdValidator("");
        personalIdValidator("0");
        personalIdValidator("1");
        personalIdValidator("123");
        personalIdValidator("-123");
        personalIdValidator("--123");
        personalIdValidator("abc");
        personalIdValidator("1a2b3c");
        personalIdValidator("‎٤‎٥‎٦‎٧‎٨‎");
    }

    public static void personalIdValidator(String personalIdNo) {
        if (personalIdNo.matches("-?[0-9]+")) {
            System.out.println("The input is only int");
        } else {
            System.out.println("The input is not only int");
        }
    }
}

// The input is not only int
// The input is only int
// The input is only int
// The input is only int
// The input is only int
// The input is not only int
// The input is not only int
// The input is not only int
// The input is not only int

#1


2  

Your check is almost correct, you just need to replace personalIdNo.matches("[0-9]") with either personalIdNo.matches("[0-9]+") or personalIdNo.matches("[0-9]*"), depending on whether you want the empty string to be valid as well.

您的检查几乎是正确的,您只需要将personalIdNo.matches(“[0-9]”)替换为personalIdNo.matches(“[0-9] +”)或personalIdNo.matches(“[0-9] * “),取决于您是否希望空字符串也有效。

Explanation:

Your current pattern means "one single character between 0 and 9". Literally "one single character", so e.g. "1" or "4" would be valid inputs even with your code. To allow for more than one character, there are two options:

您当前的模式意味着“0到9之间的单个字符”。字面意思是“单个字符”,例如即使您的代码,“1”或“4”也是有效的输入。要允许多个字符,有两个选项:

  1. + means "one or more characters"
  2. +表示“一个或多个字符”

  3. * means "zero or more characters" (i.e. allowing the empty string as well)
  4. *表示“零个或多个字符”(即允许空字符串)

  5. (technically you could also specify a concrete number with {min,max}, but I don't think that's what you want)
  6. (从技术上讲,你也可以用{min,max}指定一个具体数字,但我认为这不是你想要的)

Note that this pattern currently also prevents negative numbers. If you want to also allow negative numbers, you could use personalIdNo.matches("-?[0-9]+"), where ? means "zero or one occurrences".

请注意,此模式目前也可以防止负数。如果你还想允许负数,你可以使用personalIdNo.matches(“ - ?[0-9] +”),在哪里?表示“零或一次出现”。

Proof/Code sample:

public class NumberTest {

    public static void main(String[] args) {
        personalIdValidator("");
        personalIdValidator("0");
        personalIdValidator("1");
        personalIdValidator("123");
        personalIdValidator("-123");
        personalIdValidator("--123");
        personalIdValidator("abc");
        personalIdValidator("1a2b3c");
        personalIdValidator("‎٤‎٥‎٦‎٧‎٨‎");
    }

    public static void personalIdValidator(String personalIdNo) {
        if (personalIdNo.matches("-?[0-9]+")) {
            System.out.println("The input is only int");
        } else {
            System.out.println("The input is not only int");
        }
    }
}

// The input is not only int
// The input is only int
// The input is only int
// The input is only int
// The input is only int
// The input is not only int
// The input is not only int
// The input is not only int
// The input is not only int