• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GuessGame.java:2: cannot resolve symbol

 
Matt Player
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I am a newbie to Java and programming altogether. So far I seem to be doing okay but i've inputted one example from by book "head first java"

the error has come up as;

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();
^
6 errors

Cheers for the help,

Matt.

Also, the book says that it is for people with a knowledge of programming but there seems to be a definate lack of books for ABSOLUTE idiot beginners who don't know their arse from their integer.
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt,

Welcome to JavaRanch. One quick note - it will make your code more readable if you put it inside the code tags (and it adds line numbers for readability). But, to get to your question:

The guess game is referencing a Player object. But, the compiler cannot find that Player class? Have you created it? Is it in the same folder as the GuessGame class (it can be in a different location and then imported, but since you're just starting out, we'll start simple)?

Hope this helps,
Jeff
 
Matt Player
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not elsewhere, this is the entirity of the example. I'll have to have another look at the book. Cheers, Matt.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Please use code tags when you post source code.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This entire piece of code has already been posted in earlier posts.
here
here

all search results
 
Matt Player
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the replies, unfortunately no-one gave me the actual solution which was to turn the page and carry on reading, you doughnut!!, Anywhoo, I now have a file called GuessGame.java which has the above code, then a file Player.java ;



and another file called GameLauncher.java ;



I have been able to compile Player.java but have not been able to compile GameLauncher.java or GuessGame.java, the error message is;

GuessGame.java:65: class Player is public, should be declared in a file named Player.java
public class Player {
^
GuessGame.java:74: class GameLauncher is public, should be declared in a file named GameLauncher.java
public class GameLauncher {
^
2 errors


Sorry to be such a pain but any help would be very welcome.

Cheers, Matt.
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GuessGame.java:65: class Player is public, should be declared in a file named Player.java
public class Player {
^
GuessGame.java:74: class GameLauncher is public, should be declared in a file named GameLauncher.java
public class GameLauncher {
^
2 errors


Basically, the Player and GameLaucher classes are public classes, and need to be in there own files -- particularly Player.java and GameLauncher.java respectively.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cannot have "public class..." more than once in a given file. further, whatever comes after the word 'class' has to match the file's name (minus the .java extension) exactly - including case, even if you are on an OS that is case insensitive.
 
Matt Player
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GameLauncher.java , GuessGame.java and Player.java are all in their own files. I am trying to compile them separately but the only one I can compile succesfully is Player.java, I assume because that is the only class which is not dependent on another.

I do feel a bit out of my depth with this but I am hoping it will kind of click. For example, the error says
class Player is public, should be declared in a file named Player.java
Was that not what I did when I typed in public class Player, which is in a file called Player.java
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How are you compiling them?

And what errors are you getting?

Henry
 
Matt Player
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiling through javac in terminal (in mac os x) and the error messages are as above.
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was that not what I did when I typed in public class Player, which is in a file called Player.java


Didn't you say that the Player class compiled sucessfully? Then why is there an error message shown? Or is this for compiling one of the other classes?

Henry
 
Matt Player
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is for compiling one of the other classes.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess either Player.class is not in the same folder or there is some change in the name. Check the case for Player in the java file you are trying to compile as well as the case of the filename of the .class file.
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you have Player and GameLauncher in the same File as GuessGame, and also you might have them separately but thats still wrong.
Look at the compiler messages - its saying about line 65 and 74 in GuessGame, now look at the GuessGame.java - i think it might have
at most 65 lines long from what you wrote in the first post.
Delete them and you're all set.



 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is for compiling one of the other classes.


Agreed. If you are getting complaints about Player not being in its own file, when you are compiling one of the other classes, then you probably forgot to delete the Player class from the other class file, when you "moved" it to it's own file -- you likely copied it instead.

Henry
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Matt,

I recommend you to use an IDE (Integrated development environment) to program. I has countless advances over using the terminal.

I personally use BlueJ (Open Souce Software) but there are many others.

Cheers
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olivier Legat wrote:Hello Matt,

I recommend you to use an IDE (Integrated development environment) to program. I has countless advances over using the terminal.

I personally use BlueJ (Open Souce Software) but there are many others.

Cheers
Disagree. IDES are suitable for experienced people who want to develop software quickly. Beginners should avoid them, unless they have great difficulty spelling; some IDEs have a spell-check facility. Despite its being intended for beginners, I myself think BlueJ is by no means easy to use.
 
Michal Kucera
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt

I have had the exactly same problem.
Please try to compile it from command line with command like this:
javac -cp "C:\HFJSE\02" GameLauncher.java
Run it with something like this:
javac -cp "C:\HFJSE\02" GameLauncher.java
This will create three *.class files.

NOTE: my files was saved in folder 'HFJSE\02' on the C:\ drive so change the path in your command as needed.

I hope this helps.
Mik013
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Michal Kucera

Matt Player might have got his problem sorted out by now; the thread was from January last year
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic