TheAlgorithms/Java · error · IllegalArgumentException

Input must contain only '0' and '1'. Found: {}

Error message

Input must contain only '0' and '1'. Found: {}

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java:32

    /**
     * Returns the 1's complement of a binary string.
     *
     * @param binary A string representing a binary number (e.g., "1010").
     * @return A string representing the 1's complement.
     * @throws IllegalArgumentException if the input is null or contains characters other than '0' or '1'.
     */
    public static String onesComplement(String binary) {
        if (binary == null || binary.isEmpty()) {
            throw new IllegalArgumentException("Input must be a non-empty binary string.");
        }

        StringBuilder complement = new StringBuilder(binary.length());
        for (char bit : binary.toCharArray()) {
            switch (bit) {
                case '0' -> complement.append('1');
                case '1' -> complement.append('0');
                default -> throw new IllegalArgumentException("Input must contain only '0' and '1'. Found: " + bit);
            }
        }
        return complement.toString();
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Sanitize input: strip whitespace and any 0b/0B prefix before calling.
  2. Pre-validate with a regex like ^[01]+$ and reject early.
  3. Normalize the source format upstream so only raw bits reach this method.

Example fix

// before
String comp = OnesComplement.onesComplement(raw);

// after
String clean = raw.replaceAll("\\s+", "").replaceFirst("(?i)^0b", "");
if (!clean.matches("[01]+")) throw new IllegalArgumentException("Bad binary: " + raw);
String comp = OnesComplement.onesComplement(clean);
Defensive patterns

Strategy: validation

Validate before calling

String clean = binary == null ? "" : binary.replaceAll("\\s+", "").replaceFirst("(?i)^0b", "");
if (!clean.matches("[01]+")) {
    throw new IllegalArgumentException("Input is not binary: " + binary);
}
String out = OnesComplement.onesComplement(clean);

Type guard

static boolean isPureBinary(String s) { return s != null && s.matches("[01]+"); }

Try / catch

try {
    String out = OnesComplement.onesComplement(clean);
} catch (IllegalArgumentException e) {
    // contains a non-binary char; report the offending char from the message
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/6512d8074d130e4b. Report an issue: GitHub.