Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Chapter 5 Head First Java - problem with SimpleDotCom Gamehelper SimpleDotComGame  RSS feed

Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my last post I described some confusion I had because page 110 of the book "Head First Java" mentioned copying some ready-bake code from There's a footnote at the bottom of the page instructing us to save typing by copying from there. I have the original printing of the book if that matters. But, in any case, after receiving a reply from Eric who said he just typed in the code from page 110 and everything ran fine, I tried that, too. I think I'm still missing something though because for me the program won't compile without errors.
here's what I have now after adding a class declaration to (I also tried it without the public Class SimpleDotComGame {} too).
Here are the three programs:

public class Gamehelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader ( ));
inputLine = is.readLine();
if (inputLine.length() == 0) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);
return inputLine;
public class SimpleDotCom {
int [] locationCells; //declare an integer array named locationCells
int numOfHits = 0; //initialize and declare numOfHits variable
public void setLocationCells(int [] locs) { //setLocationCells method receives an integer array from the call
locationCells = locs; //set variable locationCells (an int array) equal to the array values passed to it (in this case 2,3,4)
public String checkYourself(String stringGuess) { // checkYourself method receieves a string and returns a string. The received string is the userGuess and the returned string is the result (hit,miss, or kill)
int guess = Integer.parseInt(stringGuess); //set guess variable equal to the integer form of the userGuess passed to it from the call
String result = "miss"; //by default we set variable result to miss
for(int i=0; i < locationCells.length; i++) { //for loop loops thru array until its end
if (guess == locationCells[i]) { //see if the guess matches a cell in the array
result = "hit"; //if so, a hit is returned
numOfHits++; // also, if so, we increment hits counter
} //out of loop
if (numOfHits == locationCells.length) { //if and when the numHits is equal to the legth of the locationCells array (in other words, 3)
result = "kill"; //then we have a kill
System.out.println(result); //now we print out the results (after looping)
return result; //we return the result to the call in SimpleDotComTestDrive
} // end method
} // end class
public class SimpleDotComGame {
public static void main(String [] args) {
int numOfGuesses = 0;
Gamehelper helper = new Gamehelper();
SimpleDotCom theDotCom = new SimpleDotCom();
int randomNum = (int) (Math.random() * 5);
int [] locations = {randomNum, randomNum+1, randomNum+2};
boolean isAlive = true;

while(isAlive == true) {
String guess = helper.getUserInput("enter a number");
String result = theDotCom.checkYourself(guess);
if (result.equals("kill"))
isAlive = false;
System.out.println(numOfGuesses + " guesses");
} // close if
} // close while
} // close main
I suspect I have just been looking at this too long and have lost track of something but I would appreciate some help so that I can continue to make progress. Thanks very much in advance for your time and help.
BTW, the error message I get when compiling is: Class or interface declaration expected.
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you've been looking at it too long.
in class SimpleDotComGame close to the end at the "if" statement, you dont have an opening bracket, just a close bracket, so you have one close bracket too many.
Barbara Norway
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much A. Wolf. Next time I'll try harder to "desk check" the code more thoroughly before bothering the list. For some reason this time I thought I was misunderstanding something on a bigger scale ... beginner's insecurity, I guess.
Anyway, Thanks!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!