TheAlgorithms/Java · error · IllegalArgumentException
Encoded text contains invalid characters: {}
Error message
Encoded text contains invalid characters: {} What it means
In the multi-character decode path, HuffmanCoding walks left/right based on each bit, so every character in the encoded string must be exactly '0' or '1'. Any other character (a space, letter, '2', newline) makes the traversal direction undefined, and the library rejects it.
Source
Thrown at src/main/java/com/thealgorithms/compression/HuffmanCoding.java:226
throw new IllegalStateException("Huffman tree is empty.");
}
StringBuilder sb = new StringBuilder();
if (root.isLeaf()) {
for (char bit : encodedText.toCharArray()) {
if (bit != '0') {
throw new IllegalArgumentException("Invalid binary sequence for single-character tree.");
}
sb.append(root.ch);
}
return sb.toString();
}
Node current = root;
for (char bit : encodedText.toCharArray()) {
if (bit != '0' && bit != '1') {
throw new IllegalArgumentException("Encoded text contains invalid characters: " + bit);
}
current = (bit == '0') ? current.left : current.right;
if (current.isLeaf()) {
sb.append(current.ch);
current = root;
}
}
if (current != root) {
throw new IllegalArgumentException("Malformed encoded string: incomplete sequence ending.");
}
return sb.toString();
}
/**View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the encoded string consists solely of '0' and '1' characters before decoding.
- Sanitize the payload by stripping non-binary characters, or reject it if any are found.
- Verify the storage/transport layer preserved the exact bit string.
Example fix
// before
String dec = hc.decode(payload); // payload has stray spaces/newlines
// after
if (!payload.matches("[01]*")) {
throw new IllegalArgumentException("Encoded text must be binary (0/1 only)");
}
String dec = hc.decode(payload); Defensive patterns
Strategy: validation
Validate before calling
if (!encodedText.matches("[01]*")) {
throw new IllegalArgumentException("Encoded text must contain only 0 and 1");
}
String dec = hc.decode(encodedText); Prevention
- Sanitize or reject payloads containing non-binary characters.
- Verify the transport layer preserved the exact bit string.
- Strip whitespace only if it is known to be cosmetic, else reject.
When it happens
Trigger: Calling decode("0 1 0") (spaces); decode("abc"); decode("012") (contains '2'); decode on a string that includes a newline or whitespace.
Common situations: The encoded payload was stored/transmitted and picked up extra characters; a copy-paste introduced whitespace; the payload was never validated as binary before decoding.
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
- Character '%c' (U+%04X) not found in Huffman dictionary.
- Invalid binary sequence for single-character tree.
- Malformed encoded string: incomplete sequence ending.
- Input string cannot be null or empty.
- Original index must be between 0 and {}, got: {}
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/2aa73f9fba257ac8.
Report an issue: GitHub.