aspose file tools*
The moose likes Beginning Java and the fly likes Code errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code errors" Watch "Code errors" New topic
Author

Code errors

Andrew James King
Greenhorn

Joined: Jun 16, 2005
Posts: 5
I am useing the book "Head First Java" 2nd edition trying to teach myself Java programing
Right now I am just trying to construct a game called SimpleDotCom. I don't know if it is somthing I have missed in the reading but every time I try to compile this code I get three errors I don't under stand My code is identical to the code from the book. Is there any way to help me out and get me back on the right track?
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) { ""the first error is here the compiler is telling me the : is wrong""
if (guess == cell) {
result = "hit";
numOfHits++;
break;
}
} // out of loop
if (numOfHits == locationCells.length) {
result ="kill";
}
System.out.println(result);
return result; ""Second error is here illegal start of expression""
} // close method ""Last one in here illegal start of expression""
} // close class

What did I do wrong that it won't compile?
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
In the "for" loop you need to give more precise condition..syntaxwise correct

in ur case probably the code needs to be as

for(int cell =0;cell<locationCells.length;cell++)
{

}


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Andrew James King
Greenhorn

Joined: Jun 16, 2005
Posts: 5
Thanks alot that work perfectly
I think my problem is that I am still using Mac OS X Panter and not Tiger so I don't have Java 5.0
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
welcome and thnks for the reply
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

i've not studied 1.5, but i think what the code has is what's called the 'enhanced for loop'. and yes, it only works in 1.5 or later.

as a side note, in the future, if you'd post the actual error message along with your code, it makes it easier to help you. also, if you'd use the UBB code tags, your formatting will be preserved, and your code will be easier to read (there are nifty little buttons down below where you make your post that will insert these tags into your post, then just paste your code between them)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Originally posted by Andrew James King:
Thanks alot that work perfectly
I think my problem is that I am still using Mac OS X Panter and not Tiger so I don't have Java 5.0


Yes that is your problem. The for each loop was introduced in JDK1.5.0, if you are learning from a book that includes the latest features then I would strongly advise you download the latest version of the JDK.
Andrew James King
Greenhorn

Joined: Jun 16, 2005
Posts: 5
Thanks for your help I am upgrading my system to the new Mac OS X Tiger that has the new Java 1.5 built in
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Does that mean.. the for loop in ur original code works as it is for the latest specifications u have mentioned ?
Andrew James King
Greenhorn

Joined: Jun 16, 2005
Posts: 5
Mac OS X Panther (OS X 10.4) has Java 1.4.2 built in to the system but will not run the higher version of Java 1.5. The newest version of OS X called Tiger (OS X 10.5) has Java 1.5 built in to the system. By up grading to the newer system my origanal code will compile as it was useing the new for loop coding.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code errors
 
Similar Threads
Beginner Ques. from Head Start Java book
Head first Java; simpledotcom problems...
Getting 6 errors with the use of result(noob)
Help compiling Program
HeadFirst SimpleDotCom compile