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);
    }
    //
}
3
  • 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. Commented Oct 7, 2017 at 11:40
  • I am new to java and i am doing my best Commented Oct 7, 2017 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. Commented Oct 7, 2017 at 13:28

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

i did it but still having an error " cannot invoke length on the array type int[]"
what error are you getting and where are you getting it?
this is the error cannot invoke length on the array type int[] and i am getting it on this line int rows = input.length, cols = input[0].length() ;
Use cols = input[0].length - remove those parenthesis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.