I made a class named Matrix in which with the help of few methods I can take order of matrix and elements of the matrix as input from the user. When I run the code it asks me to enter the order of the matrix and then instead of asking me to input the elements of the matrix it gives an error "java.lang.ArrayIndexOutOfBoundsException". You can look at the code.
import java.util.Scanner;
public class Matrix {
int mRow;
int nColumn;
Scanner input = new Scanner(System.in);
void getInput() {
System.out.println("Enter number of rows:");
mRow = input.nextInt();
System.out.println("Enter number of columns:");
nColumn = input.nextInt();
}
int a[][] = new int[mRow][nColumn];
void getElement() {
System.out.println("Enter the elements of the matrix: ");
for (int i = 0; i < mRow; i++) {
for (int j = 0; j < nColumn; j++) {
a[i][j] = input.nextInt();
}
}
}
void showMatrix() {
for (int i = 0; i < mRow; i++) {
for (int j = 0; j < nColumn; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
[1]: https://i.stack.imgur.com/guyRk.png