Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Error in compiling enhanced For loop



So, I've got the latest version of Java SE and I'm attempting to compile a small class with an enhanced for loop.

First, here's the class.

public class simpleDotCom {
int[] locationCells;
int numOfHits = 0;

public void setLocationCells(int[] locs) {
locationCells = locs;
}

public String checkYourself(String stringGuess) {
int guess = Integer.parseInt(stringGuess);
String result = "miss";

for (int cell : locationCells) {
if (guess == cell) {
result = "hit";
numOfHits++;
break;
}
}

if (numOfHits == locationCells.length) {
result = "kill";
}
System.out.println(result);
return result;
}

}

Here is the error I'm getting.

./simpleDotCom.java:13: ';' expected
               for (int cell : locationCells) {
                             ^

I'm new to Java, but not to programming and I just can't seem to figure out why this isn't working.

I've looked over the docs on the enhanced for loop and looked over plenty of articles on the proper usage... I think I'm doing it right. Not to mention this code is straight out of a book I'm using to learn Java.

Why doesn't this compile?

Thanks!