简体   繁体   中英

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. For example, 1111-1111 would be valid, but 111p-1111 or 11111-1111 would not be. However, after I have written this method, it comes out as true even when the ID is of the form 111p-1111.

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:

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
    • {4} Repeat last token 4 times (matches 4 digits)
  • - Match a hyphen literally
  • \\d Match a digit 0-9
    • {4} Repeat last token 4 times (matches 4 digits)

Your if statements only look at one number to determine if it sets the boolean to true. So if any of the numbers in each half are valid, the boolean will be set to true.

You are probably better off using regular expressions. regexr.com is a great resource to get started! :)

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:

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.

boolean firstHalf = true;
boolean secHalf = true;

and therefrom use firstHalf &= true for your updates and use a else{ firstHalf = false; } else{ firstHalf = false; } branch.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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