Nimesh
Nimesh

Reputation: 39

Matrix Multiplication, Index Out of Bounds Exception

I have try to create a code that can solve a matrix.

import java.util.Scanner;

public class Pratical_8 {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // Taking A Matrix
    System.out.println("Enter number of Rows and Column for first Matrix");
    System.out.print("Enter Rows ");
    int aRLen=sc.nextInt();
    System.out.print("Enter Columns ");
    int aCLen=sc.nextInt();
    int [][] a = new int [aRLen][aCLen];
    System.out.println("Enter Matrix");
    for(int i=0;i<aRLen;i++)
    {
        for(int j=0;j<aCLen;j++)
        {
            a[i][j]=sc.nextInt();
        }//End of j loop
    }//End of I loop

    // Taking B Matrix
    System.out.println("Enter number of Rows and Column for second Matrix");
    System.out.print("Enter Rows ");
    int bRLen=sc.nextInt();
    System.out.print("Enter Columns ");
    int bCLen=sc.nextInt();
    int [][] b = new int [bRLen][bCLen];
    System.out.println("Enter Matrix");
    for(int i=0;i<bRLen;i++)
    {
        for(int j=0;j<bCLen;j++)
        {
            b[i][j]=sc.nextInt();
        }//End of j loop
    }//End of I loop

    // Creating resulting Matrix
    int [][] r = new int [aRLen][bCLen];

    // Check for Valid input
    if(aCLen!=bRLen)
    {
        System.out.println("Error invalid input");
        System.out.println("Multiplication of this matrix is not possible");

    }
    else
    {
        for(int i=0;i<aCLen;i++)
        {
            for(int j=0;j<bRLen;j++)
            {
                for(int k=0; k<bRLen;k++)
                {
                    r[i][j] += a[i][k] * b[k][j]; // LINE 57 ERROR
                }//End of k Loop
                System.out.print(r[i][j]+" ");
            }//End of j loop
            System.out.println();
        }//End of I loop
     }//End of if-else
  }//End of main
}//End of class

it have an error after sucessfully give an output

Output:-

Enter number of Rows and Column for first Matrik

Enter Rows 1

Enter Columns 3

Enter Matrix 1 2 3

Enter number of Rows and Column for second Matrix

Enter Rows 3

Enter Columns 1

Enter Matrix 1 2 3

14 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Pratical_8.main(Pratical_8.java:57)

Process finished with exit code 1

This Error only ocure in non Sysmetrix Matrix

What is the PROBLEM?

Upvotes: 0

Views: 289

Answers (1)

Kuntal Gorai
Kuntal Gorai

Reputation: 26

Since the final matrix is of order aRLen x bCLen the last for loops needs to be modified.

for(int i=0;i<aRLen;i++)  // Instead of aClen
        {
            for(int j=0;j<bCLen;j++) //Instead of bRlen
            {
                for(int k=0; k<bRLen;k++)
                {
                    r[i][j] += a[i][k] * b[k][j]; // LINE 57 ERROR
                }//End of k Loop
                System.out.print(r[i][j]+" ");
            }//End of j loop
            System.out.println();
        }//End of I loop

This will work properly.

Upvotes: 1

Related Questions