Hey,
I'm having a problem with some code examples from Head First Java, by the O'Reilly people. It's meant to be a simple game like battleship, except without a grid and just a single line.
I'm using Eclipse.
I'm having the problem with the "setLocationCells" method (I'm correct in calling it a method I think.) (Also, please excuse me if I'm getting the terminology wrong, I'm still at the very n00b stage of learner.)
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;
}
}
System.out.println(result);
return result;
}
}
Here setLocationCells creates a method to create an array called "locs" with the next line causing locationCells array to equal the locs array.
The for loop that follows goes through the location array cells and if the cell equals a guess, it marks out a "hit."
public class SimpleDotComGame {
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); //this line is the one that creates the problem
boolean isAlive = true;{ //there's a second problem here, or at least I don't understand it.
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("You took " + numOfGuesses + " guesses");
}
}
}
}
The line that causes the problem is "theDotCom.setLocationCells(locations);" What it's supposed to be doing here is going to the SimpleDotCom class, and setting the theDotCom object's locs array/locationCells array to the same as the "locations" array from the second example of code. Which should be the randomNum, randomNum+1, randomNum+2 array elements.
However, I get an error in Eclipse. Which says;
Multiple markers at this line
- Syntax error on token "location", VariableDeclaratorId expected after
this token
- Syntax error on token(s), misplaced construct(s)
I don't get what that means. Surely the locations array is a set of numbers. Locations was the name of the array so doesn't the array name send the actual array numbers?
A second problem with this is that I don't understand why I need the parenthesis after "boolean isAlive = true;" But if I don't put it in I get an error. I'm just declaring an instance variable's value, so why the need for the things you use with conditional stuff?
The error that comes up if I take the parenthesis out is;
Multiple markers at this line
- Watchpoint:SimpleDotComGame [access and modification]
- isAlive
- Syntax error on token ";", { expected after this token
Help would be appreciated. There's a lot going on, and I'm having trouble getting my brain around everything at once. But if I look at how one thing relates to another I can generally see the problem. I just can't manage it here.