Answer to Question #162624 in Java | JSP | JSF for aj

Question #162624

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 


1
Expert's answer
2021-02-15T00:07:29-0500
    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;
    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog