{"record":{"id":"01b32d2ebb411ee5","repo":"TheAlgorithms/Java","slug":"input-must-be-a-non-empty-binary-string","errorCode":null,"errorMessage":"Input must be a non-empty binary string.","messagePattern":"Input must be a non-empty binary string\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java","lineNumber":24,"sourceCode":" *            The class OnesComplement computes the complement of binary number\n *            and returns\n *            the complemented binary string.\n * @return the complimented binary string\n */\npublic final class OnesComplement {\n    private OnesComplement() {\n    }\n\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":6,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java#L6-L38","documentation":"Thrown by OnesComplement.onesComplement(String) when the input is null or empty. The method inverts each bit of a binary string; with no characters there is nothing to invert, so null and empty strings are rejected before processing begins.","triggerScenarios":"Calling onesComplement(null) or onesComplement(\"\"). Also triggered by a value that becomes empty after trim(), or a field/config that was never set.","commonSituations":"User input left blank; reading from a property file where the key is absent (null); trimming whitespace-padded input down to empty.","solutions":["Validate the string is non-null and non-empty before calling.","Treat empty input as a no-op/empty result upstream rather than passing it through.","Provide a default non-empty binary string when the source is missing."],"exampleFix":"// before\nString out = OnesComplement.onesComplement(inputField.getText());\n\n// after\nString raw = inputField.getText();\nString out = (raw == null || raw.isEmpty()) ? \"\" : OnesComplement.onesComplement(raw);","handlingStrategy":"validation","validationCode":"if (binary == null || binary.isEmpty()) {\n    return \"\"; // or throw your own error\n}\nString out = OnesComplement.onesComplement(binary);","typeGuard":"static boolean isNonEmptyBinary(String s) { return s != null && !s.isEmpty(); }","tryCatchPattern":"try {\n    String out = OnesComplement.onesComplement(binary);\n} catch (IllegalArgumentException e) {\n    out = \"\"; // nothing to invert\n}","preventionTips":["Normalize input upstream: treat null/empty as empty output.","Avoid passing raw user fields without a null/empty check.","Document that this method needs at least one bit."],"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"}