aspose file tools*
The moose likes Beginning Java and the fly likes GuessGame Errors From Head First Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GuessGame Errors From Head First Java " Watch "GuessGame Errors From Head First Java " New topic
Author

GuessGame Errors From Head First Java

Andrew Bayliss
Greenhorn

Joined: Jan 15, 2004
Posts: 4
Keep getting the following error 'unable to resolve symbol' error, when I compile code?? Code obtained from HeadFirst Java GuessGame. Can anybody help. Great book .
GuessGame.java:2: cannot resolve symbol
symbol : class Player
location: class GuessGame
Player p1;
^
GuessGame.java:3: cannot resolve symbol
symbol : class Player
location: class GuessGame
Player p2;
^
GuessGame.java:4: cannot resolve symbol
symbol : class Player
location: class GuessGame
Player p3;
^
GuessGame.java:7: cannot resolve symbol
symbol : class Player
location: class GuessGame
p1 = new Player();
^
GuessGame.java:8: cannot resolve symbol
symbol : class Player
location: class GuessGame
p2 = new Player();
^
GuessGame.java:9: cannot resolve symbol
symbol : class Player
location: class GuessGame
p3 = new Player();
Here is the code. Thanks.
public class GuessGame {
Player p1;
Player p2;
Player p3;

public void startGame() {
p1 = new Player();
p2 = new Player();
p3 = new Player();

int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;

boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;

int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of a number between 0 and 9...");
while(true) {
System.out.println("Number to guess is " + targetNumber);

p1.guess();
p2.method();
p3.guess();

guessp1 = p1.number;
System.out.println("Player one guessed" + guessp1);

guessp2 = p2.number;
System.out.println("Player two guessed" + guessp2);

guessp3 = p3.number;
System.out.println("Player three guessed" + guessp3);

if (guessp1 == targetNumber) {
p1isRight = true;
}
if (guessp2 == targetNumber) {
p2isRight = true;
}
if (guessp3 == targetNumber) {
p3isRight = true;
}

if (p1isRight || p2isRight || p3isRight) {
System.out.println("We have a winner!");
System.out.println("Player one got it right" + p1isRight);
System.out.println("Player two got it right" + p2isRight);
System.out.println("Player three got it right" + p3isRight);
System.out.println("Game over");
break;
} else {
System.out.println("try again");
}
}
}
}
^
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Andrew,
Welcome to JavaRanch!
I don't have the book, but just based on the error message, there's apparently another class named "Player" that you have to compile at the same time; it belongs in another file named Player.java, or perhaps in this same file (if Player is not a public class.)


[Jess in Action][AskingGoodQuestions]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy, yes, Ernest is right... on page 37 is the code for the GuessGame, which relies on the "Player" class, which is at the top of page 38. A tiny thing... here it is:
Public class Player {
int number = 0; // where the guess goes
}
public void guess() {
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}
And then the third class is the launcher:
public class GameLauncher {
public static void main(String [] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}
=====================
Be sure that all three classes are in separate class files, each with a file name matching the name of the public class:
GuessGame.java
Player.java
GameLauncher.java
And be sure that they are all three in your directory when you're compiling. For now, you can still use:
cd directoryWhereJavaFilesAre
javac *.java
Cheers!
-Kathy
Andrew Bayliss
Greenhorn

Joined: Jan 15, 2004
Posts: 4
Thanks to the both of you for your help. I am just in the process of building the other 2 files. When building the class files should I run the GameLauncher.java file first and then Player.java and finally GuessGame.java file to ensure that this file compiles without throwing errors? I am new to java, been building web apps with Perl and ASP for the last few years.
Andrew Bayliss
Greenhorn

Joined: Jan 15, 2004
Posts: 4
Ok Tried to compile all 3 files and received following errors
F:\java\scripts\guessgame>javac *.java
Player.java:1: 'class' or 'interface' expected
Public class Player {
^
Player.java:5: 'class' or 'interface' expected
public void guess() {
^
Player.java:9: 'class' or 'interface' expected
}
^
Player.java:11: 'class' or 'interface' expected
^
GuessGame.java:24: cannot resolve symbol
symbol : method guess ()
location: class Player
p1.guess();
^
GuessGame.java:25: cannot resolve symbol
symbol : method method ()
location: class Player
p2.method();
^
GuessGame.java:26: cannot resolve symbol
symbol : method guess ()
location: class Player
p3.guess();
^
7 errors

Any ideas why. I copied your code for the files called Player.java and GameLauncher.java to ensure that no mistyping crept in.
Thanks again for your help.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

When you compile, there is no need to compile in a specific order; the copiler will look for all referenced classes and compile them if needed (and if it can find the source from which to compile them...) So if you compiled PLayer, it would only compile Player, as there are no references to other classes (well, except for the Math and System classes, but those are already compiled for you...). If you compiled GuessGame, it would compile GuessGame and Player, since GuessGame references Player. If you compiled GameLauncher, it would compile GameLauncher and GuessGame, because it has a reference to GuessGame. And by compiling GuessGame, it would compile Player as mentioned above. So compiling GameLauncher will have the net effect of actually compile all three classes.
When you want to run the program, you should use the command:
java GameLauncher
because that is the one with the "main" method. (If you tried to run one of the others, the JVM would complain because there is no "main" method) Running the GameLauncher will automatically load the files that it needs.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Normally you won't have problems if all .java files are visible to the compiler. Java is really good about forward referencing in most cases. The typical behavior of a Java compiler is that if one class you're compiling needs/references another class, the compiler searches for that class in its classpath. If it can't find the class, it looks to see if it can find a .java file that matches, and it goes ahead and compiles that too. There are a few rare scenarios in which the order can bite you, but usually *that* can be fixed by compiling all of them together using javac *.java. And just make sure that your *current* directory is ALWAYS on the classpath. In other words, the "." has to be part of your classpath, either set as part of an environment variable, or at the command line.
I have to give the disclaimer that you won't normally want to use javac *.java in your real world development because it's potentially expensive to recompile everything in a directory, so normally you'd specify only the things you want. But these files are tiny, and for now, we just want you to be able to get them working.
Later on you'll learn about organizing your source and class files into different directories, and using packages, etc. For now, don't worry about that. Just be sure you can compile multiple files in the same directory, and we'll go from there
cheers,
Kathy
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824


should be

(lower-case "p" and remove the "}")
Make those changes and try re-compiling.
[ January 16, 2004: Message edited by: Joel McNary ]
Andrew Bayliss
Greenhorn

Joined: Jan 15, 2004
Posts: 4
Thanks to Ernest, Kathy & Joel for your answers. I was able to fix the issue with your help. Such a stupid syntax mistake caused my error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GuessGame Errors From Head First Java