Search code examples
javaarraysfor-loopmultidimensional-arraynested-loops

Issue printing the 2D array index in Java, print statement returns 0 everytime instead of index for both row & column and also does not increase


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


Solution

  • 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. Note that in Java the elements of an int array are implicitly initialized to 0 (zero). Hence the prompt that your code prints for the user to enter a value always displays zero, e.g.

    Enter Row & Column value 0
    

    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