{"record":{"id":"3c423867e2c82b98","repo":"TheAlgorithms/Java","slug":"the-input-matrix-cannot-be-jagged-3c4238","errorCode":null,"errorMessage":"The input matrix cannot be jagged","messagePattern":"The input matrix cannot be jagged","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java","lineNumber":40,"sourceCode":"        return isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length;\n    }\n\n    private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {\n        return isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length;\n    }\n\n    public static void validateInputMatrix(double[][] matrix) {\n        if (matrix == null) {\n            throw new IllegalArgumentException(\"The input matrix cannot be null\");\n        }\n        if (matrix.length == 0) {\n            throw new IllegalArgumentException(\"The input matrix cannot be empty\");\n        }\n        if (!hasValidRows(matrix)) {\n            throw new IllegalArgumentException(\"The input matrix cannot have null or empty rows\");\n        }\n        if (isJaggedMatrix(matrix)) {\n            throw new IllegalArgumentException(\"The input matrix cannot be jagged\");\n        }\n    }\n\n    private static boolean hasValidRows(double[][] matrix) {\n        for (double[] row : matrix) {\n            if (row == null || row.length == 0) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n    /**\n     * @brief Checks if the input matrix is a jagged matrix.\n     * Jagged matrix is a matrix where the number of columns in each row is not the same.\n     *\n     * @param matrix The input matrix\n     * @return True if the input matrix is a jagged matrix, false otherwise","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java#L22-L58","documentation":"Thrown by MatrixUtil.validateInputMatrix when isJaggedMatrix detects rows of differing lengths. Matrix operations assume a rectangular matrix; ragged rows break column indexing. The check runs after null, empty, and row-validity checks.","triggerScenarios":"Passing a double[][] whose rows have different lengths (e.g., { {1,2}, {3,4,5} }), a parser that did not normalize line lengths, or manual construction with a mismatched inner array size.","commonSituations":"Inconsistent input line lengths, refactoring copy errors, or assembling a matrix from heterogeneous sources without uniform width.","solutions":["Normalize all rows to a common width before the call.","Make the parser enforce equal-length rows or reject ragged input.","Log each row's length to find the mismatch."],"exampleFix":"// before\ndouble[][] m = { {1,2}, {3,4,5} };\nMatrixUtil.validateInputMatrix(m);\n\n// after\nint width = Arrays.stream(m).mapToInt(r -> r.length).max().getAsInt();\ndouble[][] rect = new double[m.length][width];\nfor (int i=0;i<m.length;i++) System.arraycopy(m[i],0,rect[i],0,m[i].length);\nMatrixUtil.validateInputMatrix(rect);","handlingStrategy":"validation","validationCode":"int w = matrix[0].length;\nfor (double[] row : matrix) {\n    if (row.length != w) throw new IllegalStateException(\"Jagged matrix\");\n}","typeGuard":"static boolean isRectangular(double[][] m) {\n    if (m == null || m.length == 0) return false;\n    int w = m[0].length;\n    for (double[] row : m) if (row.length != w) return false;\n    return true;\n}","tryCatchPattern":null,"preventionTips":["Normalize row widths in the parser.","When merging sources, pad short rows to a common width."],"tags":["matrix","precondition","jagged-array","linear-algebra"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}