Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner Ques. from Head Start Java book  RSS feed

 
John Cristiano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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;
}

}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.)
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37242
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I redirected it Java 5 and now it compiles as it should...thanks
 
red rived
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!