Home > Software engineering >  Issue printing the 2D array index in Java, print statement returns 0 everytime instead of index for
Issue printing the 2D array index in Java, print statement returns 0 everytime instead of index for

Time:01-28

For Example:

OUTPUT that I want:

Enter Row 0 & Column 0 value

OR

Enter Row & Column value 00

CODE

import java.util.Scanner;
public class Main {
public static void main(String[] args) {

    int [][] num = new int[2][2];
    int row = 0, column = 0, numbering = 0;


    for(row = 0; row < num.length;row  ) {
        for (column = 0; column < num.length; column  ) {
            System.out.println("Enter Row & Column value "   num[row][column]);
            Scanner sc = new Scanner(System.in);
            num[row][column] = sc.nextInt();
        }
    }

    for(int i = 0; i < num.length; i  ) {
        for(int j = 0; j < num.length; j  ){
            System.out.println("The Values at "     numbering   " Row & column is: "   num[i][j]);
        }
    }

}
}

CODE OUTPUT

OUTPUT

CodePudding user response:

You are trying to print the value at the current row and column index of the 2D array, but the array has not been initialized yet so it will print 0. On this line

System.out.println("Enter Row & Column value "   num[row][column]);

Try using something like this:

System.out.println("Enter Row "   row   " & Column "   row   " value ");

CodePudding user response:

From the code in your question, i.e. from the first for loop:

System.out.println("Enter Row & Column value "   num[row][column]);

You are printing the value of the array element rather than the indexes. I suggest using method printf rather than method println as follows:

System.out.printf("Enter Row & Column value %d,%d%n", row, column);

In the second for loop you want to print both the indexes and the value of the array element at those indexes. Again, I suggest using method printf.

System.out.printf("The Values at Row %d & column %d is: %d%n", i, j, num[i][j]);

This means that you do not require local variable numbering.

Also, you should create the Scanner before the first for loop because you only need to create a Scanner object once and not in every loop iteration.

Here is my rewrite of your code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[][] num = new int[2][2];
        int row = 0, column = 0;
        Scanner sc = new Scanner(System.in);
        for (row = 0; row < num.length; row  ) {
            for (column = 0; column < num[row].length; column  ) {
                System.out.printf("Enter Row & Column value %d,%d%n", row, column);
                num[row][column] = sc.nextInt();
            }
        }
        for (int i = 0; i < num.length; i  ) {
            for (int j = 0; j < num.length; j  ) {
                System.out.printf("The Values at Row %d & column %d is: %d%n", i, j, num[i][j]);
            }
        }
    }
}

Note that in Java an array may be jagged. Hence this code gives the number of elements in a given row of the 2D array:

num[row].length

Sample run:

Enter Row & Column value 0,0
11
Enter Row & Column value 0,1
22
Enter Row & Column value 1,0
33
Enter Row & Column value 1,1
44
The Values at Row 0 & column 0 is: 11
The Values at Row 0 & column 1 is: 22
The Values at Row 1 & column 0 is: 33
The Values at Row 1 & column 1 is: 44
  • Related