{"record":{"id":"2aa73f9fba257ac8","repo":"TheAlgorithms/Java","slug":"encoded-text-contains-invalid-characters","errorCode":null,"errorMessage":"Encoded text contains invalid characters: {}","messagePattern":"Encoded text contains invalid characters: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/HuffmanCoding.java","lineNumber":226,"sourceCode":"            throw new IllegalStateException(\"Huffman tree is empty.\");\n        }\n\n        StringBuilder sb = new StringBuilder();\n\n        if (root.isLeaf()) {\n            for (char bit : encodedText.toCharArray()) {\n                if (bit != '0') {\n                    throw new IllegalArgumentException(\"Invalid binary sequence for single-character tree.\");\n                }\n                sb.append(root.ch);\n            }\n            return sb.toString();\n        }\n\n        Node current = root;\n        for (char bit : encodedText.toCharArray()) {\n            if (bit != '0' && bit != '1') {\n                throw new IllegalArgumentException(\"Encoded text contains invalid characters: \" + bit);\n            }\n\n            current = (bit == '0') ? current.left : current.right;\n\n            if (current.isLeaf()) {\n                sb.append(current.ch);\n                current = root;\n            }\n        }\n\n        if (current != root) {\n            throw new IllegalArgumentException(\"Malformed encoded string: incomplete sequence ending.\");\n        }\n\n        return sb.toString();\n    }\n\n    /**","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/HuffmanCoding.java#L208-L244","documentation":"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.","triggerScenarios":"Calling decode(\"0 1 0\") (spaces); decode(\"abc\"); decode(\"012\") (contains '2'); decode on a string that includes a newline or whitespace.","commonSituations":"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.","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."],"exampleFix":"// before\nString dec = hc.decode(payload); // payload has stray spaces/newlines\n\n// after\nif (!payload.matches(\"[01]*\")) {\n    throw new IllegalArgumentException(\"Encoded text must be binary (0/1 only)\");\n}\nString dec = hc.decode(payload);","handlingStrategy":"validation","validationCode":"if (!encodedText.matches(\"[01]*\")) {\n    throw new IllegalArgumentException(\"Encoded text must contain only 0 and 1\");\n}\nString dec = hc.decode(encodedText);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["compression","huffman","binary","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}