{"record":{"id":"8f746b7d3edc8394","repo":"TheAlgorithms/Java","slug":"input-is-not-a-valid-binary-number","errorCode":null,"errorMessage":"Input is not a valid binary number.","messagePattern":"Input is not a valid binary number\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/BinaryToOctal.java","lineNumber":24,"sourceCode":"    private static final int DECIMAL_BASE = 10;\n\n    private BinaryToOctal() {\n    }\n\n    /**\n     * This method converts a binary number to an octal number.\n     *\n     * @param binary The binary number\n     * @return The octal number\n     * @throws IllegalArgumentException if the input is not a valid binary number\n     */\n    public static String convertBinaryToOctal(int binary) {\n        if (binary == 0) {\n            return \"0\";\n        }\n\n        if (!String.valueOf(binary).matches(\"[01]+\")) {\n            throw new IllegalArgumentException(\"Input is not a valid binary number.\");\n        }\n\n        StringBuilder octal = new StringBuilder();\n        int currentBit;\n        int bitValueMultiplier = 1;\n\n        while (binary != 0) {\n            int octalDigit = 0;\n            for (int i = 0; i < BITS_PER_OCTAL_DIGIT && binary != 0; i++) {\n                currentBit = binary % DECIMAL_BASE;\n                binary /= DECIMAL_BASE;\n                octalDigit += currentBit * bitValueMultiplier;\n                bitValueMultiplier *= BINARY_BASE;\n            }\n            octal.insert(0, octalDigit);\n            bitValueMultiplier = 1; // Reset multiplier for the next group\n        }\n","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/BinaryToOctal.java#L6-L42","documentation":"Thrown by BinaryToOctal.convertBinaryToOctal(int) when the input's decimal string representation does not match the regex [01]+, meaning it contains a digit other than 0 or 1. This is a whole-input regex check, stricter than per-digit checks in sibling classes. The special case binary == 0 returns \"0\" before the regex runs.","triggerScenarios":"Passing 102, 123, or any int with a digit 2-9. Passing a negative number (the minus sign fails the regex). Passing a number whose string form has non-binary digits.","commonSituations":"Confusing decimal int with binary representation. Negative binary inputs (not supported here). Leading zeros lost when stored as int.","solutions":["Validate the input int with String.valueOf(binary).matches(\"[01]+\") before calling.","For string-based binary input with leading zeros or signs, parse manually: Integer.toString(Integer.parseInt(s, 2), 8).","Confirm the input is genuinely a binary-coded decimal integer, not a computed value."],"exampleFix":"// before\nString oct = BinaryToOctal.convertBinaryToOctal(102); // '2' fails regex\n\n// after\nString oct = BinaryToOctal.convertBinaryToOctal(1010); // valid binary digits","handlingStrategy":"validation","validationCode":"if (!String.valueOf(binary).matches(\"[01]+\")) {\n    throw new IllegalArgumentException(\"not a valid binary number: \" + binary);\n}\nString oct = BinaryToOctal.convertBinaryToOctal(binary);","typeGuard":"static boolean isBinaryNumberInt(int n) {\n    return String.valueOf(n).matches(\"[01]+\");\n}","tryCatchPattern":"try {\n    String oct = BinaryToOctal.convertBinaryToOctal(n);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"Invalid binary input: \" + n, e);\n}","preventionTips":["Validate the int's decimal digits are 0/1 before calling.","Negative numbers are rejected; handle sign separately.","Use a string-based parse (Integer.toString(Integer.parseInt(s,2),8)) for leading zeros."],"tags":["conversions","binary","octal","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}