TheAlgorithms/Java · error · IllegalArgumentException

Input data contains invalid characters. Only uppercase A-Z a

Error message

Input data contains invalid characters. Only uppercase A-Z are allowed.

What it means

Thrown by MonoAlphabetic.encrypt(String, String) when the data string does not match the regex [A-Z]+. The cipher maps uppercase A-Z positions to key characters via charToPos (c - 'A'), so lowercase letters, digits, spaces, or punctuation are all rejected.

Source

Thrown at src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java:12

package com.thealgorithms.ciphers;

public final class MonoAlphabetic {

    private MonoAlphabetic() {
        throw new UnsupportedOperationException("Utility class");
    }

    // Encryption method
    public static String encrypt(String data, String key) {
        if (!data.matches("[A-Z]+")) {
            throw new IllegalArgumentException("Input data contains invalid characters. Only uppercase A-Z are allowed.");
        }
        StringBuilder sb = new StringBuilder();

        // Encrypt each character
        for (char c : data.toCharArray()) {
            int idx = charToPos(c); // Get the index of the character
            sb.append(key.charAt(idx)); // Map to the corresponding character in the key
        }
        return sb.toString();
    }

    // Decryption method
    public static String decrypt(String data, String key) {
        StringBuilder sb = new StringBuilder();

        // Decrypt each character
        for (char c : data.toCharArray()) {
            int idx = key.indexOf(c); // Find the index of the character in the key

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Convert input to uppercase and strip/filter non-letters before encrypting.
  2. Pre-validate with data.matches("[A-Z]+") and reject otherwise.
  3. Decide on a normalization policy (drop vs. fail) for non-letters upstream.

Example fix

// before
String cipher = MonoAlphabetic.encrypt(plaintext, key);

// after
String clean = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
if (clean.isEmpty()) throw new IllegalArgumentException("No A-Z chars to encrypt");
String cipher = MonoAlphabetic.encrypt(clean, key);
Defensive patterns

Strategy: validation

Validate before calling

String clean = data == null ? "" : data.toUpperCase().replaceAll("[^A-Z]", "");
if (clean.isEmpty()) {
    throw new IllegalArgumentException("No A-Z characters to encrypt");
}
String cipher = MonoAlphabetic.encrypt(clean, key);

Type guard

static boolean isUpperAlpha(String s) { return s != null && s.matches("[A-Z]+"); }

Try / catch

try {
    String cipher = MonoAlphabetic.encrypt(data, key);
} catch (IllegalArgumentException e) {
    // non-A-Z present; normalize to uppercase and retry
}

Prevention

When it happens

Trigger: Passing data containing any character outside A-Z, including lowercase, whitespace, numbers, or punctuation. The check requires at least one character and all must be uppercase letters.

Common situations: Forgetting to uppercase user input; passing sentences with spaces; numbers or punctuation in the plaintext; empty string also fails ([A-Z]+ requires at least one char).

Understand the failure class

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/ea6a88c804edd5e3. Report an issue: GitHub.