Home > Blockchain >  trouble understanding passing 2d array to methods and displaying
trouble understanding passing 2d array to methods and displaying

Time:10-30

I am new to java, in the 11th week of classes. With this program I am supposed to use methods to pass through to obtain user input to fill a 3x4 2d array. Then add the sum of the columns and display the results. The int[][] grid = fillArray(); has an error that in[][] required. Why am I unable to call my method in the main. This is how the book says to do it along witih countless youtube videos. Please help me understand.

public class SumOfColumns {

public static int sumColumn(int[][] m, int columnIndex) {
    for (int column = 0; column < 4; column  ) {
        columnIndex = 0;
        for (int row = 0; row < 3; row  ) {
            columnIndex  = m[column][row];
        }
    }
    return columnIndex;
}

public static void main(String[] args) {
    
   ***int[][] grid = fillArray();***
}
public static int[][] fillArray(int[][] userInput) {
    Scanner input = new Scanner(System.in);
     int[][] grid = new int[3][4];
    System.out.println("Enter a 3x4 grid of numbers please: ");
    for (int row = 0; row < grid.length; row  ) {
        for (int column = 0; column < grid[row].length; column  ) {
            grid[row][column] = input.nextInt();
        }
    }
    return grid;
}

}

CodePudding user response:

When you declare your fillArray function you gave it an input of int[][] userInput, but then when you call:

int[][] grid = fillArray();

you don't give fillArray and input of int[][]. If you remove the paramter from the fillArray function that should fix it. It would look like:

public static int[][] fillArray() {
    // your code...
}
  • Related