A program in Java to check whether a string is a valid password or not.

In the above Java program to check whether a string is a valid password or not, we created a method is_Valid_Password() and called it into the main method. inside the is_Valid_Password() function we will check all the conditions on the basis of the conditions given.

import java.util.Scanner;
public class Password {  
public static final int p_length = 10;
public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print(
                "1. A password must have at least ten characters.\n" +
                "2. A password consists of only letters and digits.\n" +
                "3. A password must contain at least two digits \n" +
                "Input a password (You are agreeing to the above Terms and Conditions.): ");
        String str = input.nextLine();

        if (is_Valid_Password(str)) {
            System.out.println("Password is valid: " + str);
        } else {
            System.out.println("Not a valid password: " + str);
        }

    }

public static boolean is_Valid_Password(String password) {

        if (password.length() < p_length) return false;

        int charCount = 0;
        int numCount = 0;
        for (int i = 0; i < password.length(); i++) {

            char ch = password.charAt(i);

            if (is_Numeric(ch)) numCount++;
            else if (is_Letter(ch)) charCount++;
            else return false;
        }


        return (charCount >= 2 && numCount >= 2);
    }

    public static boolean is_Letter(char ch) {
        ch = Character.toUpperCase(ch);
        return (ch >= 'A' && ch <= 'Z');
    }


    public static boolean is_Numeric(char ch) {

        return (ch >= '0' && ch <= '9');
    }

}
Sample Output:
1. A password must have at least ten characters.
2. A password consists of only letters and digits.
3. A password must contain at least two digits 
Input a password (You are agreeing to the above Terms and Conditions.): valid23password
Password is valid: valid23password