How to take user input in 2D Array without specifying the second dimension i.e the column dimension? [duplicate]

I have used the Scanner class for taking input, this is the code.

int [] [] arr = new int[3][];
        for (int row = 0; row < arr.length; row++)
        {
            for (int col = 0; col < arr[row].length; col++) {
                arr[row][col] = sc.nextInt();
            }
        }
        for (int row = 0; row < arr.length; row++) {
            System.out.println(Arrays.toString(arr[row]));
        }

1 2 3 4 5 6 7 8 95 52 45 – Input I provided
This is the output I’m getting

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "arr[row]" is null at
com.Arrays.TwoDArray.main(TwoDArray.java:26)

You are getting the error because for int[3][], the column defaults to null. So trying to get the length throws an NPE. Either specify a second dimension or you can do it as follows.

This way uses streams to create each row.

It presumes you are typing each row of input on the same line.

  • read in a line of numbers, separated by one or more spaces
  • split that line on those spaces and convert the items to an int and place in an array. (filtering out any blank entries allows you to start the input anywhere on the line).
  • then assign that array to the original location.
Scanner sc = new Scanner(System.in);
int [] [] arr = new int[3][];
for (int row = 0; row < arr.length; row++)
{      
    String line = sc.nextLine();
    arr[row] = Arrays.stream(line.split("\\s+"))
         .filter(s->!s.isBlank())
         .mapToInt(Integer::parseInt).toArray();
}
for (int row = 0; row < arr.length; row++) {
    System.out.println(Arrays.toString(arr[row]));
}

for input like

 1 2 3 4
2 3 4
              1

prints

[1, 2, 3, 4]
[2, 3, 4]
[1]

An alternative method would be to allocate a large array and count the values as they are entered. Then use something like Arrays.copyOfRange() to copy the filled portion of the large array to its proper destination. In that case you would need some way to signal the end of the input for each line.

And finally you could just prompt for each dimension and then scan in that many items.

You are getting this error because of you didn’t defined second dimension of Array and you have used arr[row].length

Here are two way to correct this :

1.

int [] [] arr = new int[3][3];
        for (int row = 0; row < arr.length; row++)
        {
            for (int col = 0; col < arr[row].length; col++) {
                arr[row][col] = sc.nextInt();
            }
        }
        for (int row = 0; row < arr.length; row++) {
            System.out.println(Arrays.toString(arr[row]));
        }
  1. int [] [] arr = new int[3][];
    for (int row = 0; row < arr.length; row++)
    {
    for (int col = 0; col < 3; col++) {
    arr[row][col] = sc.nextInt();
    }
    }
    for (int row = 0; row < arr.length; row++) {
    System.out.println(Arrays.toString(arr[row]));
    }

Leave a Comment