Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Java simple method error

Posted on 2012-03-09
12
334 Views
Last Modified: 2012-03-09
Why does this cause an error?

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;
			 		}
			 }
		}
	}

Open in new window

0
Comment
Question by:Kyle Hamilton
  • 9
  • 3
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37703741
array[i] is one-dimensional array
beaciues array is two dimensional array

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37703746
you provbable meant somethimng like that - cpomparing int's

 if (i == row && j == col)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37703748
I guesss this is how it should be
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];
			 		}
			 }
		}
	}
                                  

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 47

Expert Comment

by:for_yan
ID: 37703752
still does not make much sense , why jsust not to return

array[row][col]  ?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37703754
one thing is clear - you cannot write this:
array[i] == row

Open in new window

beacuse array[i] is one-dimensional int array int[]

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
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 37703785
I'm just staring to learn Java, so please pardon my ignorance...

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;
	}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37703800
>I want to find the row where my coordinates (row, col) reside...

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?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37703807
if you want the answer to the last question - then you cannot have method which returns one int value which could give answer to that last question.
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37703830
in order to return the coordinates of the array element which has a specified value you need to define a new class , say class Coordinates which will have two fields - int row and int col and you can have a method which will return the instance of such class

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;
}







}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37703859
This is a reall example how this would compile and work
(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; }


}

Open in new window


Output:
1 , 2

Open in new window


indeed in the array we initialized in the beginning of the class
array[1][2] = 6
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 37703884
Hi for_yan,

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;
	}

Open in new window


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.....
0
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 37703887
Thank you so much... I need all the help I can get!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delete Android all or certain applications data programmatically 9 74
css selector 1 21
Bot application - advice 3 38
junit as external jar or library 7 24
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question