Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: for loop, incompatible types

I'm trying to run this for loop;

        for (int col= 0; grid[0].length; col++)

However every time I try to compile I get an error stating 'incompatible types - found int but expected boolean'

I can't work out what I'm doing wrong!

like image 922
Troy Avatar asked Mar 04 '10 09:03

Troy


4 Answers

the second statement: grid[0].length is an integer. The second statement in a for loop is a condition statement and needs to be a boolean.

If you're trying to loop while col is less than the length of grid[0], then you need this as your second statement:

col < grid[0].length;

like image 53
topher-j Avatar answered Oct 16 '22 07:10

topher-j


for (int col= 0; col < grid[0].length; col++)   // See the typo
like image 23
fastcodejava Avatar answered Oct 16 '22 05:10

fastcodejava


grid[0].length is the integer that the message refered to. A boolean value was expected there:

col < grid[0].length
like image 2
b.roth Avatar answered Oct 16 '22 06:10

b.roth


You need to change your code to something like:
for (int col= 0; col<grid[0].length; col++)

like image 1
sateesh Avatar answered Oct 16 '22 06:10

sateesh