-2

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();
        }
    }
}

enter image description here

  [1]: https://i.stack.imgur.com/guyRk.png

3 Answers3

0

Make sure to update the array when you get input, ie.

void getInput() {
    System.out.println("Enter number of rows:");
    mRow = input.nextInt();
    System.out.println("Enter number of columns:");
    nColumn = input.nextInt();
    a = new int[mRow][nColumn]; // you need this line
}
Adam Bates
  • 421
  • 4
  • 7
0

This code

int a[][] = new int[mRow][nColumn];

issuing done outside of any method. At the time that it is being done, the values of mRow and nColumn are zero

Move this code into a method, after getInput has returned

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

Your program should look like this.

public class Solution {
int mRow;
int nColumn;

Scanner input = new Scanner(System.in);
int a[][] ;
void getInput() {
    System.out.println("Enter number of rows:");
    mRow = input.nextInt();
    System.out.println("Enter number of columns:");
    nColumn = input.nextInt();
    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++) {
            int value = input.nextInt();
            a[i][j] = value;
        }
    }
}

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();
  }
  }

public static void main(String[] args){
    Solution solution = new Solution();
    solution.getInput();
    solution.getElement();
    solution.showMatrix();
}
}

you were keeping your array declaration outside any method so your array was looking like this a[] by changing above code it looks like this a[[0, 0]]

Doctor Who
  • 747
  • 1
  • 5
  • 28