Be seen. Boost your question’s priority for more expert views and faster solutions

Why does this cause an error?

I'm getting "Incompatible operand types int[] and int" on line 6

I'm getting "Incompatible operand types int[] and int" on line 6

```
public static int whichShip(int[][] array, int row, int col){
//find which ship got hit (find coordinate in compBoard array, and return the index)
int i = 0;
for (i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if (array[i] == row && array[i][j] == col){
return i;
}
}
}
}
```

```
public static int whichShip(int[][] array, int row, int col){
//find which ship got hit (find coordinate in compBoard array, and return the index)
int i = 0;
for (i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if (i== row && j == col){
return arry[i][j];
}
}
}
}
```

I want to find the row where my coordinates (row, col) reside...

I changed my code to this, which is no longer causing an error (though I'm not sure that this is what I'm getting at yet...)

```
public static int whichShip(int[][] array, int row, int col){
//find which ship got hit (find coordinate in compBoard array, and return the index)
int result = 0;
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if (i == row && array[i][j] == col){
result = i;
}
}
}
return result;
}
```

Sorry I am not sure I undersatnd

you have two-dimensional array of int

this array for any two coordinates - row and column will return one value - which sits in the cell with this row and this column so for row = 2 and col = 1 it can return one value

which will be the value stored ion the element array[2][1] -and that may be any int value stored in that element

to solve this task you don't need to go through array just arry[row][col] wioll give you the result.

if you wnat to resolve the opposite question, Say I have int value 12 - which cell of my grid represented by array[row][col] is this value sitting?

is this waht you want?

because the answer for this question contains not one int number , but two int numbers - you need to return from this method both row and col

for example

class Coordinates {

int row;

int col;

public Coordinates(int row, int col){

this.row = row;

this.col = col;

}

public int getRow() { return row; }

public int getColumn() { return col; }

}

once you define such method which will return instance of class Cooordinates

corresponding to the location in our array where the value has certain number

for example let's find coordinates of the eleemt whose value is 12:

then you may have something like below:

```
int value = 12;
Coordinates coords = getCoords(array, value);
System.out.println(coords.getRow() + " ," + coords.getColumn());
public Coordinates getCoords(int [][] array, int value){
for(int j=0;j<array.length; j++){
for(int i=0; i<array[j].length; i++){
if(array[j][i] == value){
Coordinates coords = new Coordinates(j, i);
return coords;
}
}
}
return null;
}
}
```

(see output below)

```
public class Miscellaneous {
static int[][] array = new int[][]{{1,2,3},{4,5,6} };
public static Coordinates getCoords(int [][] array, int value){
for(int j=0;j<array.length; j++){
for(int i=0; i<array[j].length; i++){
if(array[j][i] == value){
Coordinates coords = new Coordinates(j, i);
return coords;
}
}
}
return null;
}
public static void main(String[] args) {
int value = 6;
Coordinates coords = getCoords(array, value);
System.out.println(coords.getRow() + " , " + coords.getColumn());
}
}
class Coordinates {
int row;
int col;
public Coordinates(int row, int col){
this.row = row;
this.col = col;
}
public int getRow() { return row; }
public int getColumn() { return col; }
}
```

Output:

```
1 , 2
```

indeed in the array we initialized in the beginning of the class

array[1][2] = 6

I completely screwed up what I was trying to achieve. And hence, it made no sense :)

I have since figured it out, but it's totally dependent on other stuff in the program and the method I wrote in the end has little to do with my original code:

```
public static int whichShip(int[][] array, int row, int col){
//find which ship got hit (find coordinate in compBoard array, and return the index)
int result = 0;
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if(array[i][j] == (row * 10) + col){
result = i;
break;
}
}
}
return result;
}
```

I want to give you credit for steering me away from what I was doing... thus I will accept one of your answers even though I didn't actually use it.

Thank you so much.....

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Open in new window

Open in new window

and row is just int

so no comparison beteenn them can be made - this is a compiler error - rethink what you wanted to express