{"record":{"id":"6512d8074d130e4b","repo":"TheAlgorithms/Java","slug":"input-must-contain-only-0-and-1-found","errorCode":null,"errorMessage":"Input must contain only '0' and '1'. Found: {}","messagePattern":"Input must contain only '0' and '1'\\. Found: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java","lineNumber":32,"sourceCode":"\n    /**\n     * Returns the 1's complement of a binary string.\n     *\n     * @param binary A string representing a binary number (e.g., \"1010\").\n     * @return A string representing the 1's complement.\n     * @throws IllegalArgumentException if the input is null or contains characters other than '0' or '1'.\n     */\n    public static String onesComplement(String binary) {\n        if (binary == null || binary.isEmpty()) {\n            throw new IllegalArgumentException(\"Input must be a non-empty binary string.\");\n        }\n\n        StringBuilder complement = new StringBuilder(binary.length());\n        for (char bit : binary.toCharArray()) {\n            switch (bit) {\n                case '0' -> complement.append('1');\n                case '1' -> complement.append('0');\n                default -> throw new IllegalArgumentException(\"Input must contain only '0' and '1'. Found: \" + bit);\n            }\n        }\n        return complement.toString();\n    }\n}\n","sourceCodeStart":14,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java#L14-L38","documentation":"Thrown by OnesComplement.onesComplement(String) when the string contains a character other than '0' or '1'. The method's switch statement only handles those two bits; any other character (letter, space, '0b' prefix, newline) hits the default branch and is reported with the offending character.","triggerScenarios":"Passing a string with whitespace, a '0b' binary prefix, lowercase/letters, or stray punctuation. The message echoes the exact bad character (the {} placeholder is filled with the offending bit).","commonSituations":"Pasting formatted binary with separators (e.g. \"1010 0011\"); accepting a value prefixed with 0b from a parser; mixed-case or hex digits slipping in.","solutions":["Sanitize input: strip whitespace and any 0b/0B prefix before calling.","Pre-validate with a regex like ^[01]+$ and reject early.","Normalize the source format upstream so only raw bits reach this method."],"exampleFix":"// before\nString comp = OnesComplement.onesComplement(raw);\n\n// after\nString clean = raw.replaceAll(\"\\\\s+\", \"\").replaceFirst(\"(?i)^0b\", \"\");\nif (!clean.matches(\"[01]+\")) throw new IllegalArgumentException(\"Bad binary: \" + raw);\nString comp = OnesComplement.onesComplement(clean);","handlingStrategy":"validation","validationCode":"String clean = binary == null ? \"\" : binary.replaceAll(\"\\\\s+\", \"\").replaceFirst(\"(?i)^0b\", \"\");\nif (!clean.matches(\"[01]+\")) {\n    throw new IllegalArgumentException(\"Input is not binary: \" + binary);\n}\nString out = OnesComplement.onesComplement(clean);","typeGuard":"static boolean isPureBinary(String s) { return s != null && s.matches(\"[01]+\"); }","tryCatchPattern":"try {\n    String out = OnesComplement.onesComplement(clean);\n} catch (IllegalArgumentException e) {\n    // contains a non-binary char; report the offending char from the message\n}","preventionTips":["Strip whitespace and 0b prefixes before calling.","Pre-validate with ^[01]+$ and reject early with a helpful error.","Reject mixed-case/hex at the input boundary."],"tags":["bit-manipulation","java","validation","string","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}