首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用字符密码Java返回正确代码的问题

使用字符密码Java返回正确代码的问题
EN

Stack Overflow用户
提问于 2018-06-08 05:36:16
回答 2查看 344关注 0票数 0

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

我需要程序列出密码的所有要求,如果他们没有得到满足。

例如:

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

应该返回:"Enter termination“

代码语言:javascript
复制
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";
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-08 05:58:26

您可以使用此验证函数

代码语言:javascript
复制
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";
}
票数 1
EN

Stack Overflow用户

发布于 2018-06-08 05:46:47

您的passCheck方法遍历所有字符,如果其中一个字符不能满足您的要求,则返回一个结果。另一种方法是假设输入是无效的,直到所有需求都被完全满足:

代码语言:javascript
复制
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";
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50750501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档