1

I created a method that needs to check the range of a multidimensional array and make sure each value in the 2D array is smaller than or equal to the length of the array.

public static boolean method(int[][] solution){
        //check rows for 1-N
        for (int i=0; i < solution.length; i++){
                if (solution[i] > solution.length)
                return false; //check rows
            }

        //check columns for 1 - N
        for (int j = 0; j<solution.length; j++){
            //get a column in the one dimensional array
            int[] column = new int[solution.length];
            for (int i=0; i < solution.length; i++){
                column[i] = solution[i][j];
            }
            if (column[i] > solution.length)
                return false; //check columns

        }
    return true;
    }

However, the errors I receive are follows:

Program.java:99: error: bad operand types for binary operator '>'
                                if (solution[i] > solution.length)
                                                ^
  first type:  int[]
  second type: int
Program.java:110: error: cannot find symbol
                        if (column[i] > solution.length)
                                   ^
  symbol:   variable i
  location: class Program
2 errors

Perhaps for the first error i need to get the array value instead of comparing the array? Not sure..

4 Answers 4

4

First, you can't compare an array with an int value

if (solution[i] > solution.length) // solution is a 2-d array and thus solution[i] is a 1-d array
// which can't be compared with an int value

and second, the i was declared in the for loop, and thus it's not visible outside its scope.

for (int i=0; i < solution.length; i++){
    column[i] = solution[i][j];
} // scope of i is over here
if (column[i] > solution.length) // that's why you can't access `i` here
    return false;

You either need to use j or declare the i before the for, so that you can use it after it or probably, move the if inside the inner for loop(and that seems to be the right way for your problem as well).

Sign up to request clarification or add additional context in comments.

5 Comments

i thought solution[i] is comparing the value inside the array to the int?
oh what a dumb mistake on the second error. sorry its been a long night
so how can i compare the value INSIDE the array to the interger value solution.length?
I'm really not sure what the first for loop does. Can you tell what exactly you want to do, so that I can suggest accordingly?!
I feel that the first for is not at all required, as you don't need to compare rows separately as such. With 2 nested for loops, you can iterate over all the rows & columns of the 2-d array and do the comparison.
1

You are using i outside the for loop, maybe you want to use j instead.

Comments

0

Its because, solution is a 2d array.

int[][] solution;

and to get this work it should be,

if(solution[][] > solution.length)

Comments

0

I suppose this much code should suffice

public static boolean method(int[][] solution) {
    // check rows for 1-N
    for (int i = 0; i < solution.length; i++) {
        for (int j = 0; j < solution[i].length; i++) {
            if (solution[i][j] > solution.length)
                return false; // check rows
        }
    }

    return true;
}

1 Comment

that's correct. but i was trying to follow something in my book.

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.