• 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

Beginner Ques. from Head Start Java book

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In chapter 5 you are guided through writing code for an extremely basic battleship game, however when I can't get the code o compile(the text says that it should). Below is the code, and I receive 8 errors and 3 warnings when trying to compile.

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;
}

}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

Welcome to JavaRanch!

This code would require Java 5 (aka "Tiger", aka JDK 1.5). It would give some strange errors with JDK 1.4 or earlier. Type

javac -J-version

(that's javac space dash J dash version)

and let us know what you get. If it says anything about "J2SDK1.4" then you'll need to go to java.sun.com and download the newest Java release (new is relative -- Tiger has been the standard for several years now.)
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
It compiles for me. In the future, please post the errors you get so it is easier to diagnose the problem.

Try typing "javac -version" at the command line. Does it say that you are using Java 1.5? I suspect your path is pointing to Java 1.4 causing the Java 5 features not to compile.
 
John Cristiano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I redirected it Java 5 and now it compiles as it should...thanks
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.. sorry to hijack your thread.. im new.. how to compile your code?

Thanks!
 
John Cristiano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure you get the SDK from Sun's website, then at the terminal type:
javac <filename>
and the compiler should give you a class file. If you are running windows you will have to set your path variable so the javac command will start the compiler.
 
reply
    Bookmark Topic Watch Topic
  • New Topic