简体   繁体   中英

Matrix Multiplication Returing Array Index out of bound error

This is one first post here, Pardon me if my question doesn't meet required standards here.

I have written a piece of code which takes input for two matrix from two separate files and performs multiplication and output the data to a new file.

It gives perfect output for 2x3 or 3x3 matrix. If i give input of 4x4 matrix i get array index out of bound runtime exception. I don't understand the reason as i dynamically create index

I get an array index out of bound exception at line 40.

I get an error.

![Snipet][2]

  1. List item

     public class MM { private BufferedReader br; private int sum = 0; private final static String matrixA="matrixA.txt"; private final static String matrixB="matrixB.txt"; public static void main(String[] args) { new MM().MathMultiplicationValues(matrixA, matrixB); } private void MathMultiplicationValues(String mat1, String mat2) { try { br = new BufferedReader(new FileReader(mat1)); String line; int mat1rows = 0, mat1cols = 0, mat2rows = 0, mat2cols = 0; while ((line = br.readLine()) != null) { mat1cols = line.split(" ").length + 1; mat1rows++; } br.close(); // To close file br = new BufferedReader(new FileReader(mat2)); // to read input from file. while ((line = br.readLine()) != null) { mat2cols = line.split(" ").length + 1; mat2rows++; } int[][] mat1vals = new int[mat1rows ][mat1cols ]; int[][] mat2vals = new int[mat2rows ][mat2cols ]; br.close(); br = new BufferedReader(new FileReader(mat1)); for (int i = 1; i < mat1rows + 1; i++) { line = br.readLine(); String[] colvals = line.split(" "); for (int j = 1; j < mat1cols; j++) { mat1vals[i][j] = Integer.parseInt(colvals[j - 1]); } } br.close(); br = new BufferedReader(new FileReader(mat2)); for (int i = 1; i < mat2rows + 1; i++) { line = br.readLine(); String[] colvals = line.split(" "); for (int j = 1; j < mat2cols; j++) { mat2vals[i][j] = Integer.parseInt(colvals[j - 1]); } } br.close(); if ((mat1cols-1) == mat2rows) { int[][] resltmat = new int[mat1rows + 1][mat2cols + 1]; for (int i = 1; i < mat1rows + 1; i++) { //Loop does matrix multiplication. for (int j = 1; j < mat1cols; j++) { for (int k = 0; k < mat2rows + 1; k++) sum = sum + mat1vals[i][k] * mat2vals[k][j]; resltmat[i][j] = sum; sum = 0; } } final PrintWriter pw = new PrintWriter("Answer.txt"); //Creates a new file called Matrix Answer. for (int i = 1; i < mat1rows + 1; i++) { for (int j = 1; j < mat2cols; j++) { pw.print(resltmat[i][j] + " "); // Writes the output to file the file called MatrixAnswer } pw.println(); } pw.close(); } else // If no of columns not equal to rows control passes to else block. System.out.println("Multiplication of Matrix is not possible because columns are not equal to rows"); } catch (Exception e) { e.printStackTrace(); } } } 

Might be because of this

for (int i = 1; i < mat1rows + 1; i++) {
    line = br.readLine();
    String[] colvals = line.split(" ");
    for (int j = 1; j < mat1cols; j++) {
        mat1vals[i][j] = Integer.parseInt(colvals[j - 1]);

    }

}

i = mat1rows on the last iteration which is OOB. Change for (int i = 1; i < mat1rows + 1; i++) to for (int i = 1; i < mat1rows; i++)

As you used in the allocation, the dimension of the resulting matrix are mat1rows x mat2cols . Thus in the computation of resltmat[i][j] the index i has bound mat1rows (check) and the index j has the upper bound mat2cols (fail). Thus change the range of j from mat1cols to mat2cols .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM