• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code errors

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome and thnks for the reply
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic