Skip to content
Java project #06Beginner

Password Strength Checker

Score passwords on length, character variety, common-password lists (including leetspeak), repeats and keyboard sequences, with clear explanations and tips.

Estimated time
~2 hours to build
Files
4 files · 256 lines
Technologies
Java 21, String / Character APIs, Records, Set.of

Introduction

Weak passwords cause a large share of account break-ins. This checker scores each password with transparent rules, so users understand exactly why P@ssw0rd is weak and correct-Horse-battery-7 is strong. It never prints the full password, only a masked form.

Problem statement

Read one password per line. Score each from 0 to 100: points for length (4 per character up to 16) and for each character type (lower, upper, digit, symbol); deductions for containing a common password (after undoing leetspeak such as 0→o and @→a), for runs of the same character and for alphabet or keyboard sequences. Print a masked password, the score, a rating (VERY WEAK to VERY STRONG), the reasons and improvement tips, and finally how many passwords were strong.

Objectives

  • Apply several independent rules and combine their scores
  • Use Character methods and IntStream checks
  • Detect runs and sequences in strings
  • Present results with explanations rather than just a number

Features

  • Length and variety scoring
  • Common-password detection with leetspeak normalisation
  • Repeated-character and sequence penalties
  • Ratings, reasons and tips
  • Masked output that never reveals the password

Concepts demonstrated

  • String scanning
  • Rule-based scoring
  • Normalisation
  • Records for results
  • Privacy by design

System architecture

evaluate returns a Result record with the score, rating, notes and tips. It adds length and variety points, then normalises the password (lower case, leetspeak substitutions, letters only) to compare against the COMMON set. longestRepeat finds the longest run of one character; longestSequence checks consecutive characters against the alphabet, digits and the QWERTY row, forwards and backwards. Scores are clamped to 0–100.

Folder structure

TextProject tree
password-strength-checker/
├── README.md
├── src/Main.java
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

src/Main.java

104 lines
Javasrc/Main.java
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.Set;
/**
* Password Strength Checker — scores each password line from standard input against length,
* character-variety, pattern and dictionary rules, explains every deduction, rates it and
* suggests improvements. It never stores or prints the full password, only a masked form.
*/
public class Main {
static final Set<String> COMMON = Set.of("password", "123456", "qwerty", "letmein", "welcome", "admin", "iloveyou", "monkey", "dragon", "abc123");
static final String[] SEQUENCES = {"abcdefghijklmnopqrstuvwxyz", "0123456789", "qwertyuiopasdfghjklzxcvbnm"};
record Result(int score, String rating, List<String> notes, List<String> tips) { }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 0, strong = 0;
while (in.hasNextLine()) {
String pw = in.nextLine();
if (pw.isEmpty()) continue;
count++;
Result r = evaluate(pw);
if (r.score() >= 70) strong++;
System.out.printf("%d. %-14s score %3d/100 %s%n", count, masked(pw), r.score(), r.rating());
r.notes().forEach(n -> System.out.println(" - " + n));
r.tips().forEach(t -> System.out.println(" tip: " + t));
}
System.out.println("\n" + count + " password(s) checked, " + strong + " strong or better.");
}
static Result evaluate(String pw) {
List<String> notes = new ArrayList<>(), tips = new ArrayList<>();
int score = 0;
int len = pw.length();
score += Math.min(len, 16) * 4; // up to 64 points for length
if (len < 8) { notes.add("too short (" + len + " characters, minimum 8)"); tips.add("use at least 12 characters"); }
boolean lower = pw.chars().anyMatch(Character::isLowerCase);
boolean upper = pw.chars().anyMatch(Character::isUpperCase);
boolean digit = pw.chars().anyMatch(Character::isDigit);
boolean symbol = pw.chars().anyMatch(c -> !Character.isLetterOrDigit(c));
int kinds = (lower ? 1 : 0) + (upper ? 1 : 0) + (digit ? 1 : 0) + (symbol ? 1 : 0);
score += kinds * 9; // up to 36 points for variety
if (!upper) tips.add("add an upper-case letter");
if (!digit) tips.add("add a digit");
if (!symbol) tips.add("add a symbol such as ! or #");
String lowered = pw.toLowerCase(Locale.ROOT);
// Undo common "leetspeak" substitutions (P@ssw0rd -> password) before checking the word list.
String letters = lowered.replace('0', 'o').replace('@', 'a').replace('1', 'l').replace('$', 's').replace('3', 'e').replaceAll("[^a-z]", "");
for (String word : COMMON) {
if (lowered.contains(word) || letters.equals(word)) {
score -= 40;
notes.add("contains the common password \"" + word + "\"");
tips.add("avoid dictionary words and well-known passwords");
break;
}
}
int run = longestRepeat(pw);
if (run >= 3) { score -= 10 * (run - 2); notes.add("the same character repeats " + run + " times in a row"); }
int seq = longestSequence(lowered);
if (seq >= 3) { score -= 8 * (seq - 2); notes.add("contains a keyboard or alphabet sequence of " + seq + " characters"); }
score = Math.max(0, Math.min(100, score));
String rating = score >= 85 ? "VERY STRONG" : score >= 70 ? "STRONG" : score >= 50 ? "FAIR" : score >= 25 ? "WEAK" : "VERY WEAK";
if (notes.isEmpty()) notes.add(kinds + " character types, no weak patterns found");
return new Result(score, rating, notes, score >= 85 ? List.of() : tips);
}
static int longestRepeat(String s) {
int best = s.isEmpty() ? 0 : 1, run = 1;
for (int i = 1; i < s.length(); i++) {
run = s.charAt(i) == s.charAt(i - 1) ? run + 1 : 1;
best = Math.max(best, run);
}
return best;
}
// Longest run of consecutive characters from any of the SEQUENCES, forwards or backwards.
static int longestSequence(String s) {
int best = 0;
for (String seq : SEQUENCES) {
for (String line : new String[]{seq, new StringBuilder(seq).reverse().toString()}) {
int run = 1;
for (int i = 1; i < s.length(); i++) {
int a = line.indexOf(s.charAt(i - 1)), b = line.indexOf(s.charAt(i));
run = a >= 0 && b == a + 1 ? run + 1 : 1;
best = Math.max(best, run);
}
}
}
return best;
}
static String masked(String pw) {
if (pw.length() <= 2) return "*".repeat(pw.length());
return pw.charAt(0) + "*".repeat(Math.min(pw.length() - 2, 10)) + pw.charAt(pw.length() - 1);
}
}

What each file does

src/Main.java
The scoring rules in evaluate, the repeat and sequence detectors, masking and the report.

How to run it

Compile

Terminal
javac -d out $(find src -name "*.java")
# Windows PowerShell: javac -d out (Get-ChildItem -Recurse src -Filter *.java).FullName

Run

Terminal
java -cp out Main

Test against the expected output

Terminal
java -cp out Main < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt
# Windows PowerShell:
# Get-Content tests\sample-input.txt | java -cp out Main > actual.txt

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
1. a*c score 13/100 VERY WEAK
- too short (3 characters, minimum 8)
- contains a keyboard or alphabet sequence of 3 characters
tip: use at least 12 characters
tip: add an upper-case letter
tip: add a digit
tip: add a symbol such as ! or #
2. p*********3 score 14/100 VERY WEAK
- contains the common password "password"
- contains a keyboard or alphabet sequence of 3 characters
tip: add an upper-case letter
tip: add a symbol such as ! or #
tip: avoid dictionary words and well-known passwords
3. P******d score 28/100 WEAK
- contains the common password "password"
tip: avoid dictionary words and well-known passwords
4. S********6 score 67/100 FAIR
- 3 character types, no weak patterns found
tip: add a symbol such as ! or #
5. a**********B score 55/100 FAIR
- the same character repeats 4 times in a row
tip: add a symbol such as ! or #
6. q*******Z score 0/100 VERY WEAK
- contains the common password "qwerty"
- contains a keyboard or alphabet sequence of 6 characters
tip: avoid dictionary words and well-known passwords
7. c**********7 score 100/100 VERY STRONG
- 4 character types, no weak patterns found
8. T**********! score 92/100 VERY STRONG
- 4 character types, no weak patterns found
8 password(s) checked, 2 strong or better.
Input
abc
password123
P@ssw0rd
Summer2026
aaaa1111BBBB
qwerty!9Z
correct-Horse-battery-7
Tr0ub4dour&3x!

Testing

  • Run with tests/sample-input.txt and compare with tests/expected-output.txt.
  • P@ssw0rd is recognised as "password" after leetspeak normalisation and rated WEAK.
  • qwerty!9Z is penalised both as a common password and as a 6-key keyboard sequence.
  • correct-Horse-battery-7 scores 100 because it is long and uses all four character types.

Common errors

P@ssw0rd isn't detected as a common password

Apply leetspeak replacements before removing non-letters; otherwise the 0 and @ are deleted first.

Scores go below zero

Clamp the final score with Math.max(0, Math.min(100, score)).

Possible improvements

  • Load a larger common-password list from a file
  • Estimate entropy in bits and time to crack
  • Check against the user's name or e-mail

Additional challenges

  1. 1Detect dates such as 2026 or 1999.
  2. 2Suggest a stronger variant of the password.
  3. 3Detect repeated substrings like 'abcabc'.

Built and tested it yourself? Mark it to update your dashboard.