TheAlgorithms/Java · error · IllegalArgumentException
Input is not a valid binary number.
Error message
Input is not a valid binary number.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/conversions/BinaryToOctal.java:24
private static final int DECIMAL_BASE = 10;
private BinaryToOctal() {
}
/**
* This method converts a binary number to an octal number.
*
* @param binary The binary number
* @return The octal number
* @throws IllegalArgumentException if the input is not a valid binary number
*/
public static String convertBinaryToOctal(int binary) {
if (binary == 0) {
return "0";
}
if (!String.valueOf(binary).matches("[01]+")) {
throw new IllegalArgumentException("Input is not a valid binary number.");
}
StringBuilder octal = new StringBuilder();
int currentBit;
int bitValueMultiplier = 1;
while (binary != 0) {
int octalDigit = 0;
for (int i = 0; i < BITS_PER_OCTAL_DIGIT && binary != 0; i++) {
currentBit = binary % DECIMAL_BASE;
binary /= DECIMAL_BASE;
octalDigit += currentBit * bitValueMultiplier;
bitValueMultiplier *= BINARY_BASE;
}
octal.insert(0, octalDigit);
bitValueMultiplier = 1; // Reset multiplier for the next group
}
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before String oct = BinaryToOctal.convertBinaryToOctal(102); // '2' fails regex // after String oct = BinaryToOctal.convertBinaryToOctal(1010); // valid binary digits
Defensive patterns
Strategy: validation
Validate before calling
if (!String.valueOf(binary).matches("[01]+")) {
throw new IllegalArgumentException("not a valid binary number: " + binary);
}
String oct = BinaryToOctal.convertBinaryToOctal(binary); Type guard
static boolean isBinaryNumberInt(int n) {
return String.valueOf(n).matches("[01]+");
} Try / catch
try {
String oct = BinaryToOctal.convertBinaryToOctal(n);
} catch (IllegalArgumentException e) {
throw new DomainException("Invalid binary input: " + n, e);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Confusing decimal int with binary representation. Negative binary inputs (not supported here). Leading zeros lost when stored as int.
Related errors
- Incorrect binary digit: {}
- Incorrect binary digit: {}
- Slope and intercept must be valid numbers.
- For input string: {}
- Input cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/8f746b7d3edc8394.
Report an issue: GitHub.