Write a Java method to check whether a string is a valid password. Each character must be stored in a character array
Password rules:
A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Expected Output:
Input a password (You are agreeing to the above Terms and Conditions.): abcd1234 Password is valid: abcd1234
public static boolean passCheck(String pass){
if (pass.length() < 10){
return false;
}
int digitsCount = 0;
for(int i = 0; i < pass.length(); i++){
char c = pass.charAt(i);
if (c >= 'a' && c <= 'z'){
//is letter
} else if (c >= 'A' && c <= 'Z'){
//is letter
} else if (c >= '0' && c <= '9'){
digitsCount++;
} else {
return false;
}
}
if (digitsCount < 2){
return false;
}
return true;
}
Comments
Leave a comment