Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Matthias Nörr
8,614 PointsCannot find symbol error. Classes saved in different java files.
I try to build a Battleship Game in Java.
The classes battleship, gameHelper and battleshipGame are stored in different java files. The Java file "battleshipGame" does not compile. The compiler gives the error messages "battleshipGame.java:18: error: cannot find symbol b.setLocationcells(locations);" and "battleshipGame.java:26: error: cannot find symbol String result = b.checkyourself(guess);"
public class battleship {
int [] locationCells;
int numOfHits = 0;
//locs ist eine Platzhalter-Variable
public void setLocationCells (int [] locs) {
locationCells = locs;
}
public String checkYourself(String stringGuess) {
//convert an inputted String ("2" in this case) to an Integer //stringGuess ist eine Platzhalter-Variable
int guess = Integer.parseInt(stringGuess);
String result = "miss";
//loop geht durch das Array --> jedes Mal wird das nächste Element des Arrays in der Variable cell abgelegt
for (int cell : locationCells) {
if (guess == cell) {
result = "hit";
numOfHits++;
//es ist ein Treffer--> den loop verlassen; neuen guess des users erwarten
break;
}
}
//wenn die Anzahl der Treffer der Länge des Arrays entspricht, dem das Objekt b zugeorndet ist, wird result zu "kill" geändert
if (numOfHits == locationCells.length) {
result = "kill";
}
//display the result to the user
System.out.println(result);
return result; //Frage: Was passiert hier?
}
}
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;
}
}
public class battleshipGame {
public static void main(String[] args) {
int numOfGuesses = 0;
//holds a method for getting input the user types in the command line
gameHelper helper = new gameHelper();
//create a battleship object
battleship b = new battleship();
//create a random number from 0 to 4.999, transform it from double to int and round it down by (int) --> no decimals
int randomNum = (int) (Math.random() * 5);
//random number is used to make an int array for the location of the Battleship (3 consecutive ints out of 7) --> now the battleship is always on an other spot
int[] locations = {randomNum, randomNum + 1, randomNum + 2};
b.setLocationcells(locations);
//create a variable to end the prompt-loop when the user killed the boat
boolean isAlive = true;
while (isAlive == true) {
//method of the gamehelper class that asks the user for commandline input, reads it in after the user hits return and gives back the result in a string
String guess = helper.getUserInput("enter a number");
String result = b.checkyourself(guess);
numOfGuesses++;
if (result.equals("kill")) {
isAlive = false;
System.out.println("You took " + numOfGuesses + " guesses");
}
}
}
}
1 Answer
Kayondo Martin
7,481 PointsThe function/method you have is setLocationCells, and you are calling setLocationcells (with a small c for cells instead of capital C)
The same is the issue with checkyourself, the method you have has capital Y instead of small one, as in checkYourself...
I hope I've helped.