使用Character Password Java返回正确代码的问题

时间:2018-06-07 21:36:16

标签: java error-handling passwords

我遇到的问题是,当有人输入包含数字,大写和小写的密码时,程序仍然表示他们需要所有密码。

如果不符合密码,我需要程序列出密码的所有要求。

例如:

A123456789aa返回:“您至少需要一个小写字母。请输入您的密码:”

应返回:“输入终止密钥”

public static void main(String[] args) {

    Scanner input = new Scanner (System.in);

    boolean condition = true;
    while (condition) {
        System.out.print("Please Enter Your Password: ");
        String password = input.nextLine();

        if (password.equals("endofinput")) {
            System.out.print("Your password is valid"); condition = false;

        } else {
            System.out.print(passCheck(password));
            condition = true;
        }
    }
    input.close();
}


public static String passCheck(String password) {
    String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,." + " ";

      if (password.length() < 8) {
        return ("You need at least 8 characters. ");

    } for (int i = 0; i < password.length(); i++) {
        if (specialChars.contains(password.substring(i)))
            return ("Your password cannot contain special characters. ");
        else if (password.equals("password"))
            return ("Your password cannot be password. ");
        else if(!Character.isUpperCase(password.charAt(i)))
            return ("You need at least one uppercase. ");
        else if (!Character.isLowerCase(password.charAt(i)))
            return ("You need at least one lowercase. ");
        else if (!Character.isDigit(password.charAt(i)))
            return ("You need at least one digit. ");   
    }
return "Enter termination key";
}

2 个答案:

答案 0 :(得分:3)

你的passCheck方法迭代所有字符,如果一个字符没有满足你的一个要求,则返回一个结果。另一种方法是假设输入无效,直到满足所有要求:

boolean foundUppercase = false;
    boolean foundLowercase = false;
    boolean foundDigits = false;
    //Dont have to do the check for password as input for every letter in the word.
    if (password.equals("password"))
        return ("Your password cannot be password. ");

    for (int i = 0; i < password.length(); i++) {
        if (specialChars.contains(password.substring(i)))
            //only in this case we can for sure say that the input is not valid
            return ("Your password cannot contain special characters. ");
        else if (Character.isUpperCase(password.charAt(i)))
            foundUppercase=true;
        else if (Character.isLowerCase(password.charAt(i)))
            foundLowercase= true;
        else if (Character.isDigit(password.charAt(i)))
            foundDigits = true;
    }
    if (!foundUppercase) {
        // uppercase letter missing
        return ("You need at least one uppercase. ");
    } else if (!foundLowercase) {
        // lower case letter missing
        return ("You need at least one lowercase. ");
    } else if (!foundDigits) {
        // missing digits
        return ("You need at least one digit. ");
    }

    return "Enter termination key";

答案 1 :(得分:1)

您可以使用此验证功能

public static String passCheck(String password) {
    //String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,." + " ";
    String expected_pattern = "^[a-zA-Z0-9]{8,}$";
    String lowercase_pattern = "(.*)[a-z]+(.*)";
    String uppercase_pattern = "(.*)[A-Z]+(.*)";
    String digit_pattern = "(.*)[0-9]+(.*)";

    if (password == null || password.length() < 8) return ("You need at least 8 characters. ");     
    if (password.toLowerCase().equals("password")) return ("Your password cannot be password. ");
    if (!password.matches(lowercase_pattern)) return ("You need at least one lowercase. ");
    if (!password.matches(uppercase_pattern)) return ("You need at least one uppercase. ");
    if (!password.matches(digit_pattern)) return ("You need at least one digit. ");
    if (!password.matches(expected_pattern)) return ("Your password cannot contain special characters. ");

    return "Enter termination key";
}