0

i have start to learn programming from scratch, now i'm in array concept. i have doubt in multidimensional array,the doubt is i'm getting the user input as matrix range and value and trying to traverse the array but i got an runtime error. this is my program:

public class Multi {

    /**
     * @param args
     */
    public static void main(String[] args)throws IOException {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int arr[][] = new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                arr[i][j] = sc.nextInt();
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
               System.out.println(arr[j][i]);
            }
            System.out.println("\n");
        }
        sc.close();
    }
}

Input: First line contains 2 space separated integers,

N total rows,

M - total columns.

Each of the next

N lines will contain

M space separated integers.

Output: Print M lines each containing N space separated integers.

(i got this error)..

3 5 
13 4 8 14 1 
9 6 3 7 21
5 12 17 9 3

Exception in thread "main" 13 9 5

java.lang.ArrayIndexOutOfBoundsException: 3

at hackerearth.Multi.main(Multi.java:24)

enter image description here

xenteros
  • 15,586
  • 12
  • 56
  • 91
AK Vignesh
  • 13
  • 2

2 Answers2

4

The solution: (explanation below)

public static void main(String[] args)throws IOException {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    int arr[][] = new int[n][m];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            arr[i][j] = sc.nextInt();
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            System.out.println(arr[i][j]);  //MODIFICATION
        }
        System.out.println("\n");
    }
    sc.close();
}

Your second loop caused the error:

Exception in thread "main" 13 9 5
java.lang.ArrayIndexOutOfBoundsException: 3
at hackerearth.Multi.main(Multi.java:24)

Your Exception says: the index that has caused is 3. The line in which it happens is line number 24 in file Multi.java. It is your second double loop.

You have switched i with j.

Your array is 3x5:

+----+----+----+
| 00 | 01 | 02 |
+----+----+----+
| 10 | 11 | 12 |
+----+----+----+
| 20 | 21 | 22 |
+----+----+----+
| 30 | 31 | 32 |
+----+----+----+
| 40 | 41 | 42 |
+----+----+----+

and the array you're trying to access in line 24 would be 5x3:

+----+----+----+----+----+
| 00 | 01 | 02 | 03 | 04 |
+----+----+----+----+----+
| 10 | 11 | 12 | 13 | 14 |
+----+----+----+----+----+
| 20 | 31 | 32 | 33 | 34 |
+----+----+----+----+----+

Accessing 03 causes the error as it's not in the first array.

xenteros
  • 15,586
  • 12
  • 56
  • 91
0

You are getting the error because in print statement you are accessing arr[j][i] rather than arr[i][j].

xenteros
  • 15,586
  • 12
  • 56
  • 91
Deepti
  • 138
  • 1
  • 8