2

I am having some truble in this part of the code cols = input[0].length() ; it says to me that 1) cannot invoke length of the primitive type of int 2)length cannot be resolved or is not field. i am doning a code for minesweeper game and here i want to fill the grid with the index from the file I have.

public static void main(String[] args) {

    Scanner file = new Scanner ("in.txt");

    int line=0;
    int m = file.nextInt();
    int n = file.nextInt();
    int index = file.nextInt();

    int [][] input = new int[m][n];
    String.valueOf(input);

    for (int [] field : input) {
        printMineField(field);
    }
    file.close();
    line= line++;
}

private static void printMineField(int[] input) {


    int rows = input.length, cols = input[0].length ;
    int[][] grid = new int[rows][cols];


    for (int i = 0; i < rows; i++) {
        Arrays.fill(grid[i], 0);
    }
    //
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
jay
  • 23
  • 2
  • Hint: Java isn't JavaScript. No point in double tagging. And then length is a field on arrays - not a method. So drop the () - and do spend some research on such basics before asking other questions. – GhostCat Oct 07 '17 at 11:40
  • I am new to java and i am doing my best – jay Oct 07 '17 at 11:57
  • Yes. But again : this is super basic and you are expected to do some real research prior posting here. Hint: anything you can dream of asking at this point of your learning curve has been ask here. And answered. – GhostCat Oct 07 '17 at 13:28

1 Answers1

1

First length is a field in array, not a function. So you should use cols = input[0].length; - remove those parenthesis.

Next, You are passing one dimensional array to your method. You should pass a two dimensional array.

Replace

private static void printMineField(int[] input) 

with:

private static void printMineField(int[][] input) 

Then you will also need to replace

for (int [] field : input) {
    printMineField(field);
}

with:

printMineField(input);
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59