我遇到的问题是,当有人输入包括数字、大写和小写的密码时,程序仍然会说他们需要所有的密码。
我需要程序列出密码的所有要求,如果他们没有得到满足。
例如:
A123456789aa返回:“您至少需要一个小写字母,请输入您的密码:”
应该返回:"Enter termination“
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";
}
发布于 2018-06-08 05:58:26
您可以使用此验证函数
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";
}
发布于 2018-06-08 05:46:47
您的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";
https://stackoverflow.com/questions/50750501
复制相似问题