aspose file tools*
The moose likes Beginning Java and the fly likes GuessGame.java:2: cannot resolve symbol Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GuessGame.java:2: cannot resolve symbol" Watch "GuessGame.java:2: cannot resolve symbol" New topic
Author

GuessGame.java:2: cannot resolve symbol

Matt Player
Greenhorn

Joined: Jan 11, 2009
Posts: 11
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

Joined: Apr 07, 2007
Posts: 230
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

Joined: Jan 11, 2009
Posts: 11
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

Joined: Aug 16, 2005
Posts: 14430
    
  23

Welcome to JavaRanch. Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

This entire piece of code has already been posted in earlier posts.
here
here

all search results


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Player
Greenhorn

Joined: Jan 11, 2009
Posts: 11
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
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matt Player
Greenhorn

Joined: Jan 11, 2009
Posts: 11
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
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40


How are you compiling them?

And what errors are you getting?

Henry
Matt Player
Greenhorn

Joined: Jan 11, 2009
Posts: 11
Compiling through javac in terminal (in mac os x) and the error messages are as above.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

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

Joined: Jan 11, 2009
Posts: 11
Yes, this is for compiling one of the other classes.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
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.


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
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
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

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

Joined: Nov 17, 2007
Posts: 176

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


Olly
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40044
    
  28
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

Joined: May 01, 2010
Posts: 1
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


Mik013 [:-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40044
    
  28
Welcome to the Ranch, Michal Kucera

Matt Player might have got his problem sorted out by now; the thread was from January last year
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GuessGame.java:2: cannot resolve symbol