Check Password Strength in Java — Rules, Examples and Program

Learn how to check the strength of a password in Java by validating length, uppercase, lowercase, digits, and special characters using character-level logic.

Check Password Strength in Java

Password strength checking is an essential concept in real-world applications like login systems, banking apps, and secure platforms.

A strong password reduces the risk of brute-force and dictionary attacks. In this program, we validate a password based on common security rules.

Password Strength Rules

A password is considered strong if it contains:

Example 1

Input: Java@123

Output: Strong Password

Example 2

Input: java123

Output: Weak Password

Logic Explanation

  1. Check password length.
  2. Traverse each character in the password.
  3. Identify uppercase, lowercase, digit, and special characters.
  4. If all conditions are satisfied, password is strong.

Java Program to Check Password Strength


import java.util.Scanner;

public class PasswordStrengthChecker {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter password: ");
        String password = sc.nextLine();

        boolean hasUpper = false;
        boolean hasLower = false;
        boolean hasDigit = false;
        boolean hasSpecial = false;

        if (password.length() < 8) {
            System.out.println("Weak Password (Minimum 8 characters required)");
            return;
        }

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

            char ch = password.charAt(i);

            if (ch >= 'A' && ch <= 'Z') {
                hasUpper = true;
            } 
            else if (ch >= 'a' && ch <= 'z') {
                hasLower = true;
            } 
            else if (ch >= '0' && ch <= '9') {
                hasDigit = true;
            } 
            else {
                hasSpecial = true;
            }
        }

        if (hasUpper && hasLower && hasDigit && hasSpecial) {
            System.out.println("Strong Password");
        } else {
            System.out.println("Weak Password");
        }
    }
}

Sample Output


Enter password: Java@123
Strong Password

Enter password: hello123
Weak Password

Important Notes

Practice Challenges

  1. Classify password as Weak, Medium, or Strong.
  2. Disallow spaces in passwords.
  3. Check for repeated characters.
  4. Enforce at least one character from a custom symbol set.

Password validation is a real-world use case of string manipulation. Mastering this logic helps build secure Java applications.