• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

error: cannot find symbol

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi code ranchers, i'm a beginner so i am having a little problem in resolving the cannot find symbol error, i'm learning from headfirst java so here is the code:

public class GuessGame {


public void startGame(){


Player p1 = new Player(); //player objects
Player p2 = new Player();
Player p3 = new Player();

int guessp1 = 0; //variable to hold players answers
int guessp2 = 0;
int guessp3 = 0;

boolean p1isright = false; //variable to hold value based on players answer
boolean p2isright = false;
boolean p3isright = false;

int targetnumber = (int) (Math.random() * 10); //sets a target number for the players to guess
System.out.println("I am thinking of a number between 0 and 9");

while(true){
System.out.println("Number to guess is .." + targetnumber);

p1.guess(); //calls each players' guess() method.
p2.guess();
p3.guess();

guessp1 = p1.number;
System.out.println("I am guessing " + guessp1); //displays the value of the players guess

guessp2 = p2.number;
System.out.println("I am guessing " + guessp2);

guessp3 = p3.number;
System.out.println("I am guessing " + guessp3);

if(guessp1 == targetnumber){ //checks if the guess is the same as the targey number and if true set the player to true
p1isright = true;
}
if(guessp3 == 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 is over.");
break; // game over, so break out of loop
}
else
//Since no one got it right we must keep going
System.out.println("Players have to try again");


}
}
}

the symbol the compiler can't find is "Player".
Thanks for your anticipated help.
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch! Here in startGame() method you're creating an object of Player class but where is the code of Player class? Compiler can't find code of Player class so giving compile time error.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and please always put your code in Code tag and read this for Java Programming Style Guide
 
Edeh Godwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay sorry, here is the player class code(i don't know how to put my code in Code tag, sorry):

public class Player{
int number = 0;
public void guess(){
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which file did you write Player class code in? If you've written code of Player class in separate file named Player.java then, are GuessGame.java file and Player.java file in same folder? If both are not in same folder then you have to import Player.java file in your GuessGame.java file. Please go through this link for how to use tags
 
Edeh Godwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, yes the Player.java and GuessGame.java files are in the same directory as well as the main class.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If both GuessGame.java and Player.java files are in the same folder then it should compile successfully but you can't run as none of them have main method. I copied your code and put in a folder and compiled it successfully. How are you compiling your code would you please clarify?
 
Edeh Godwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That's the test class with the main method, i compiled the code using my windows command prompt.

After changing the directory to where the program is in i used the command javac GuessGame.java and the result was the cannot find symbol error.
compile.jpg
[Thumbnail for compile.jpg]
compilation of the GuessGame.java
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That error message means that the Java compiler doesn't know what "Player" is. Either you don't have Player.java in the same directory, or you've set the classpath in such a way that the compiler can't find class Player.

What is your classpath set to? It's better to not set the CLASSPATH environment variable at all.
 
Edeh Godwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay thank you, so do i just delete the CLASSPATH and try recompiling?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. If you do not have the CLASSPATH environment variable set, Java takes the current directory as the classpath. It's better not to set CLASSPATH, because it's not really needed and only confuses things.
 
Edeh Godwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YAYY!! Thank you guys, i deleted the CLASSPATH environment variable and recompiled the class with the main method and the program ran. I am grateful.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic