TheAlgorithms/Java · error · IllegalArgumentException

Invalid complex number format: {num}

Error message

Invalid complex number format: {num}

What it means

Thrown by ComplexNumberMultiply.parse (called from multiply) when the input string is null or does not match the regex -?\d+\+-?\d+i. The parser expects exactly the form a+bi where a and b are integers, e.g. "1+-2i" or "3+4i". Any deviation — whitespace, decimal points, missing 'i', wrong separator — fails the regex and is rejected before Integer.parseInt runs.

Source

Thrown at src/main/java/com/thealgorithms/maths/ComplexNumberMultiply.java:14

package com.thealgorithms.maths;

/**
 * Multiplies two complex numbers represented as strings in the form "a+bi".
 * Supports negative values and validates input format.
 */
public final class ComplexNumberMultiply {

    private ComplexNumberMultiply() {
    }

    private static int[] parse(String num) {
        if (num == null || !num.matches("-?\\d+\\+-?\\d+i")) {
            throw new IllegalArgumentException("Invalid complex number format: " + num);
        }

        String[] parts = num.split("\\+");
        int real = Integer.parseInt(parts[0]);
        int imaginary = Integer.parseInt(parts[1].replace("i", ""));
        return new int[] {real, imaginary};
    }

    public static String multiply(String num1, String num2) {
        int[] a = parse(num1);
        int[] b = parse(num2);

        int real = a[0] * b[0] - a[1] * b[1];
        int imaginary = a[0] * b[1] + a[1] * b[0];

        return real + "+" + imaginary + "i";
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Format inputs to exactly a+bi with integer coefficients, e.g. "3+-2i" for 3-2i.
  2. Strip whitespace and normalise the imaginary unit to 'i' before calling.
  3. If you need fractional coefficients, pre-parse them yourself and call the arithmetic directly instead of using this string API.
  4. Build the string via String.format("%d+%di", real, imag) to guarantee the shape.

Example fix

// before
ComplexNumberMultiply.multiply("1.5 + 2i", "3+4i"); // throws

// after
ComplexNumberMultiply.multiply("1+2i", "3+4i");
// for 1.5 use a different approach or round to int first
Defensive patterns

Strategy: validation

Validate before calling

String normalised = num == null ? null : num.replace(" ", "").replace('j', 'i');
if (normalised == null || !normalised.matches("-?\\d+\\+-?\\d+i")) {
    throw new IllegalArgumentException("expected a+bi with integer coefficients, got " + num);
}
ComplexNumberMultiply.multiply(normalised, other);

Type guard

static boolean isValidComplexString(String s) {
    return s != null && s.matches("-?\\d+\\+-?\\d+i");
}

Prevention

When it happens

Trigger: Calling multiply("1+2j", ...), multiply("1.5+2i", ...), multiply("1 + 2i", ...), multiply(null, ...), multiply("1+2", ...), or any string where the real/imaginary parts are not plain integers joined by a single '+'.

Common situations: Mixing coordinate formats ('j' notation from engineering vs 'i'); floating-point coefficients where the API only accepts ints; user-typed input with spaces or parentheses; downstream of a serializer that produced 'a + b i'.

Related errors


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