简体   繁体   English

Java ID检查方法

[英]java ID check method

Im trying to write a method that takes in an ID of the form "xxxx-xxxx" (x being any number 1-9) and checks to see if the ID entered is valid. 我试图编写一个方法,该方法采用“ xxxx-xxxx”形式的I​​D(x为1-9的任意数字),并检查输入的ID是否有效。 For example, 1111-1111 would be valid, but 111p-1111 or 11111-1111 would not be. 例如,1111-1111是有效的,但111p-1111或11111-1111无效。 However, after I have written this method, it comes out as true even when the ID is of the form 111p-1111. 但是,写完此方法后,即使ID的格式为111p-1111,它也可以显示为true。

public static boolean idCheck(String ID){
    char[] idChar = ID.toCharArray();
    boolean firstHalf = false;
    boolean secHalf = false;
    for (int i = 0; i < 5; ++i){//Check first half 
        if ((idChar[i] > 47 && idChar[i] < 58)){//Checks ascii vals to see if valid ID
            firstHalf = true;
        }
    }

    for (int i = 5; i < idChar.length; ++i){//Check second half
        if ((idChar[i] > 47 && idChar[i] < 58)){//Checks ascii vals to see if valid ID
            secHalf = true;
        }
    }

    //If all values are valid, returns true.
    if (firstHalf == true && secHalf == true && idChar[4] == '-' && ID.length() == 9){
        return true;
    }

    return false;
}

Using a regular expression would be much simpler in this case: 在这种情况下,使用正则表达式会更简单:

\d{4}-\d{4}

In Java: 在Java中:

static boolean idCheck(String id) {
    return id.matches("\\d{4}-\\d{4}");
}

If you're unfamiliar with regular expressions, here's an explanation: 如果您不熟悉正则表达式,请按以下说明进行:

  • \\d Match a digit 0-9 \\d 匹配数字0-9
    • {4} Repeat last token 4 times (matches 4 digits) {4} 重复最后一个令牌4次(匹配4位数字)
  • - Match a hyphen literally - 从字面上匹配连字符
  • \\d Match a digit 0-9 \\d 匹配数字0-9
    • {4} Repeat last token 4 times (matches 4 digits) {4} 重复最后一个令牌4次(匹配4位数字)

Your if statements only look at one number to determine if it sets the boolean to true. 您的if语句仅查看一个数字来确定是否将布尔值设置为true。 So if any of the numbers in each half are valid, the boolean will be set to true. 因此,如果每一半中的any数字均有效,则布尔值将设置为true。

You are probably better off using regular expressions. 使用正则表达式可能更好。 regexr.com is a great resource to get started! regexr.com是一个很好的入门资源! :) :)

Something like: 就像是:

[1-9]{4}-[1-9]{4} (You can also use \d)

You only check if there is at least one character that matches, not if any of the input characters are failing. 您仅检查是否存在至少一个匹配的字符,而不检查是否有任何输入字符失败。

To have a quick solution that is easy to understand for any Java developer after you you could use a Regex and check if your input matches: 要获得对任何Java开发人员来说都易于理解的快速解决方案,您可以使用Regex并检查输入是否匹配:

public static boolean idCheck(String id){
  return Pattern.matches("\\d{4}-\\d{4}", id);
}

If you want to keep your way of checking you should start with true boolean s and check if they stay true. 如果要保持检查的方式,则应从true boolean s开始,并检查它们是否为true。

boolean firstHalf = true;
boolean secHalf = true;

and therefrom use firstHalf &= true for your updates and use a else{ firstHalf = false; } 然后使用firstHalf &= true进行更新,并使用else{ firstHalf = false; } else{ firstHalf = false; } branch. else{ firstHalf = false; }分支。

To keep your method I would prefer to always back out fast if you know the result: 为了保持您的方法,如果您知道结果,我宁愿总是快速退出:

public static boolean idCheck(String id)
{
    //check length
    if (id.length() != 9)
        return false;

    char[] idChar = id.toCharArray();
    //check for dash
    if (idChar[4] != '-')
        return false;

    //check first half
    for (int i = 0; i < 5; ++i)
    {
        if (!(idChar[i] > 47 && idChar[i] < 58))
            return false;
    }

    //check second half
    for (int i = 5; i <= 10; ++i)
    {
        if (!(idChar[i] > 47 && idChar[i] < 58))
            return false;
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM