user3038498
user3038498

Reputation: 13

Passing CharArray to Method

I'm trying to pass my char array grid1 to the method called status. I'm receiving the error char cannot be converted to char[][]. How would I pass grid1 so it would work in the for loop?

    for (int row = 0; row < 30; row++){
        for (int col = 0; col < 30; col ++){
            if (status(grid1[row][col], row, col)){

            }
        }
    }



    public boolean status(char [][] grid, int a, int b){

    char value = grid[a][b];    
        if (value == 'X'){
            //add X to another array
            return true;
        } else {
        return false;
        }
        //add - to another array
    }

Upvotes: 0

Views: 124

Answers (4)

b2Wc0EKKOvLPn
b2Wc0EKKOvLPn

Reputation: 2074

You're already dereferencing your grid1 array in the loop. So unless that loop is to be part of your status method for whatever reason, your status method could be a lot simpler: you don't need to pass a char[][], you could pass a char. And clearly you don't need the column and row indexes. Your method should be like this:

public boolean status(char value){

        if (value == 'X'){
            //add X to another array
            return true;
        } else {
        return false;
        }
        //add - to another array
}

Upvotes: 0

Warlord
Warlord

Reputation: 2826

grid1[row][col] is a single element of char array, therefore is of type char. You need to pass the whole array grid1.

Upvotes: 0

Liviu Stirb
Liviu Stirb

Reputation: 6075

the problem is that your method signature is expecting an array and you call it using a value from the array.

call like:

 status(grid1, row, col)

or fix the method signature

public boolean status(char grid){

    char value = grid; 

Upvotes: 1

mbroshi
mbroshi

Reputation: 979

Just replace

if (status(grid1[row][col], row, col)){

with

if (status(grid1, row, col)){

Upvotes: 0

Related Questions