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 keyView on GitHub (pinned to fdfb9a395b)
Solutions
- Convert input to uppercase and strip/filter non-letters before encrypting.
- Pre-validate with data.matches("[A-Z]+") and reject otherwise.
- 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
- Uppercase and strip non-letters upstream.
- Pre-validate with [A-Z]+ before encrypting.
- Decide drop-vs-fail for punctuation/spaces at the boundary.
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
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Input data contains invalid characters.
- Invalid Baconian code: {}
- DES key must be supplied as a 64 character binary string
- Encrypted message should be a multiple of 64 characters in l
- Base, secret, and prime must be non-null and positive values
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ea6a88c804edd5e3.
Report an issue: GitHub.