GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Chapter 5 Head First Java - problem with SimpleDotCom Gamehelper SimpleDotComGame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Chapter 5 Head First Java - problem with SimpleDotCom Gamehelper SimpleDotComGame" Watch "Chapter 5 Head First Java - problem with SimpleDotCom Gamehelper SimpleDotComGame" New topic
Author

Chapter 5 Head First Java - problem with SimpleDotCom Gamehelper SimpleDotComGame

Barbara Norway
Ranch Hand

Joined: Sep 30, 2003
Posts: 150
Hello,
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 headfirstjava.com. 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 SimpleDotComGame.java program won't compile without errors.
here's what I have now after adding a class declaration to SimpleDotComGame.java: (I also tried it without the public Class SimpleDotComGame {} too).
Here are the three programs:
Gamehelper.java:

import java.io.*;
public class Gamehelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader (System.in ));
inputLine = is.readLine();
if (inputLine.length() == 0) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);
}
return inputLine;
}
}
_________________________________________________
SimpleDotCom.java:
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
break;
}
} //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
__________________________________________________________________________
SimpleDotComGame.java:
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};
theDotCom.setLocationCells(locations);
boolean isAlive = true;

while(isAlive == true) {
String guess = helper.getUserInput("enter a number");
String result = theDotCom.checkYourself(guess);
numOfGuesses++;
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:
SimpleDotComGame.java:24: Class or interface declaration expected.
Thanks!


farmkitty
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
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

Joined: Sep 30, 2003
Posts: 150
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!
 
GeeCON Prague 2014
 
subject: Chapter 5 Head First Java - problem with SimpleDotCom Gamehelper SimpleDotComGame