{"record":{"id":"b2f710a1df324288","repo":"TheAlgorithms/Java","slug":"invalid-complex-number-format-num","errorCode":null,"errorMessage":"Invalid complex number format: {num}","messagePattern":"Invalid complex number format: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ComplexNumberMultiply.java","lineNumber":14,"sourceCode":"package com.thealgorithms.maths;\n\n/**\n * Multiplies two complex numbers represented as strings in the form \"a+bi\".\n * Supports negative values and validates input format.\n */\npublic final class ComplexNumberMultiply {\n\n    private ComplexNumberMultiply() {\n    }\n\n    private static int[] parse(String num) {\n        if (num == null || !num.matches(\"-?\\\\d+\\\\+-?\\\\d+i\")) {\n            throw new IllegalArgumentException(\"Invalid complex number format: \" + num);\n        }\n\n        String[] parts = num.split(\"\\\\+\");\n        int real = Integer.parseInt(parts[0]);\n        int imaginary = Integer.parseInt(parts[1].replace(\"i\", \"\"));\n        return new int[] {real, imaginary};\n    }\n\n    public static String multiply(String num1, String num2) {\n        int[] a = parse(num1);\n        int[] b = parse(num2);\n\n        int real = a[0] * b[0] - a[1] * b[1];\n        int imaginary = a[0] * b[1] + a[1] * b[0];\n\n        return real + \"+\" + imaginary + \"i\";\n    }\n}","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ComplexNumberMultiply.java#L1-L32","documentation":"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.","triggerScenarios":"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 '+'.","commonSituations":"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'.","solutions":["Format inputs to exactly a+bi with integer coefficients, e.g. \"3+-2i\" for 3-2i.","Strip whitespace and normalise the imaginary unit to 'i' before calling.","If you need fractional coefficients, pre-parse them yourself and call the arithmetic directly instead of using this string API.","Build the string via String.format(\"%d+%di\", real, imag) to guarantee the shape."],"exampleFix":"// before\nComplexNumberMultiply.multiply(\"1.5 + 2i\", \"3+4i\"); // throws\n\n// after\nComplexNumberMultiply.multiply(\"1+2i\", \"3+4i\");\n// for 1.5 use a different approach or round to int first","handlingStrategy":"validation","validationCode":"String normalised = num == null ? null : num.replace(\" \", \"\").replace('j', 'i');\nif (normalised == null || !normalised.matches(\"-?\\\\d+\\\\+-?\\\\d+i\")) {\n    throw new IllegalArgumentException(\"expected a+bi with integer coefficients, got \" + num);\n}\nComplexNumberMultiply.multiply(normalised, other);","typeGuard":"static boolean isValidComplexString(String s) {\n    return s != null && s.matches(\"-?\\\\d+\\\\+-?\\\\d+i\");\n}","tryCatchPattern":null,"preventionTips":["Build input strings with String.format(\"%d+%di\", real, imag) to guarantee shape.","Normalise whitespace and the imaginary unit before calling.","Reject fractional coefficients up front; the API is integer-only."],"tags":["validation","parsing","complex-numbers","regex","input-format"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}