Twodarrayops (Please check for code line 24)

(I am not able to get the error )
May I know whether the reading of an each array element is correct?

import java.util.Scanner;

public class Twodarrayops
{
static Scanner scn=new Scanner(System.in);
public static void main(String[] args)
{
int [][] array=takeinput();
display(array);
}

private static int[][] takeinput()
{
    System.out.println("Enter the number of rows ");
    int rows=scn.nextInt();
    System.out.println("Enter the number of columns ");
    int columns=scn.nextInt();
    int[][] arr=new int[rows][columns];
    System.out.println("Enter the array elements ");
    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<columns;j++)
        {
            arr[rows][columns]=scn.nextInt();

        }
        System.out.println();
    }
    return arr;

} 
private static void display(int[][] arr)
{
    for(int i=0;i<arr.length;i++)
    {
        for(int j=0;j<arr[i].length;j++)
        {
            System.out.print(arr[i][j]+" ");
        }
        System.out.println();
    }
}

}

@jdkatti,
https://ide.codingblocks.com/s/315222 corrected code.

Error:
do:

  for(int i=0;i<rows;i++)
    {
        for(int j=0;j<columns;j++)
        {
            arr[i][j]=scn.nextInt();

        }
        System.out.println();
    }

Instead of:

 for(int i=0;i<rows;i++)
    {
        for(int j=0;j<columns;j++)
        {
            arr[rows][columns]=scn.nextInt();

        }
        System.out.println();
    }

because you are using rows and columns, you will get an error

How can I get input to be in
{ 0 0 0
0 0 0
0 0 0} this format

Sorry sir, got the solution for the above problem

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.