TheAlgorithms/Java · error · IllegalArgumentException
Input must contain only '0' and '1'.
Error message
Input must contain only '0' and '1'.
What it means
Thrown by TwosComplement.twosComplement(String) when the input does not match the regex [01]+. The method first inverts all bits then adds 1 with carry, so it requires a pure binary string. Note: unlike OnesComplement, this method does NOT guard against null — calling it with null will throw NullPointerException on binary.matches(...) before this message is ever reached.
Source
Thrown at src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java:36
public final class TwosComplement {
private TwosComplement() {
}
/**
* Computes the Two's Complement of the given binary string.
* Steps:
* 1. Compute the One's Complement (invert all bits).
* 2. Add 1 to the One's Complement to get the Two's Complement.
* 3. Iterate from the rightmost bit to the left, adding 1 and carrying over as needed.
* 4. If a carry is still present after the leftmost bit, prepend '1' to handle overflow.
*
* @param binary The binary number as a string (only '0' and '1' characters allowed).
* @return The two's complement of the input binary string as a new binary string.
* @throws IllegalArgumentException If the input contains non-binary characters.
*/
public static String twosComplement(String binary) {
if (!binary.matches("[01]+")) {
throw new IllegalArgumentException("Input must contain only '0' and '1'.");
}
StringBuilder onesComplement = new StringBuilder();
for (char bit : binary.toCharArray()) {
onesComplement.append(bit == '0' ? '1' : '0');
}
StringBuilder twosComplement = new StringBuilder(onesComplement);
boolean carry = true;
for (int i = onesComplement.length() - 1; i >= 0 && carry; i--) {
if (onesComplement.charAt(i) == '1') {
twosComplement.setCharAt(i, '0');
} else {
twosComplement.setCharAt(i, '1');
carry = false;
}
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Pre-validate with regex ^[01]+$ and reject non-matching input.
- Guard for null separately, since this method throws NPE (not this message) on null.
- Strip whitespace and 0b prefixes before invoking.
Example fix
// before
String out = TwosComplement.twosComplement(input);
// after
if (input == null || !input.matches("[01]+")) {
throw new IllegalArgumentException("Require non-null binary string");
}
String out = TwosComplement.twosComplement(input); Defensive patterns
Strategy: validation
Validate before calling
if (binary == null || !binary.matches("[01]+")) {
throw new IllegalArgumentException("Require a non-null [01]+ string");
}
String out = TwosComplement.twosComplement(binary); Type guard
static boolean isValidForTwos(String s) { return s != null && s.matches("[01]+"); } Try / catch
try {
String out = TwosComplement.twosComplement(binary);
} catch (IllegalArgumentException | NullPointerException e) {
// note: null throws NPE here, not IAE
} Prevention
- Remember this method does NOT null-guard — check null yourself to avoid NPE.
- Validate contents with [01]+ before calling.
- Strip formatting/whitespace upstream.
When it happens
Trigger: Passing a string containing any non-binary character. Passing null will instead produce an NPE at the regex check, not this IllegalArgumentException.
Common situations: Forgetting that null is not handled (NPE rather than this error); passing binary with whitespace or a 0b prefix; mixed encodings.
Related errors
- Input must be a non-empty binary string.
- Input must contain only '0' and '1'. Found: {}
- Input cannot be negative
- The exponent must be positive
- Inputs cannot be null.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/e2924a7ea38b8f51.
Report an issue: GitHub.