Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code errors

 
Andrew James King
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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++)
{

}
 
Andrew James King
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome and thnks for the reply
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic