This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

cannot find symbol error  RSS feed

 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spent like 8 hours because of many errors during making that simple program, but I can't figure out these last 6 errors.

GuessGame.java:51: cannot find symbol
symbol  : method createPlayers()
location: class GuessGame
                createPlayers();
                ^
GuessGame.java:55: cannot find symbol
symbol  : variable player
location: class GuessGame
                        player[x].setPlayerNum(n);
                        ^
GuessGame.java:63: cannot find symbol
symbol  : variable TheNumber
location: class GuessGame
                        TheNumber.setNum(n);
                        ^
GuessGame.java:67: cannot find symbol
symbol  : method compareNumbers(int)
location: class GuessGame
                System.out.println("╨Я╨╡╤А╨▓╤Л╨╣ ╨╕╨│╤А╨╛╨║ ╤Г╨│╨░╨┤╨░╨╗?  " + c
ompareNumbers(0));
                                                              ^
GuessGame.java:68: cannot find symbol
symbol  : method compareNumbers(int)
location: class GuessGame
                System.out.println("╨Т╤В╨╛╤А╨╛╨╣ ╨╕╨│╤А╨╛╨║ ╤Г╨│╨░╨┤╨░╨╗?  " + c
ompareNumbers(1));
                                                              ^
GuessGame.java:69: cannot find symbol
symbol  : method compareNumbers(int)
location: class GuessGame
                System.out.println("╨в╤А╨╡╤В╨╕╨╣ ╨╕╨│╤А╨╛╨║ ╤Г╨│╨░╨┤╨░╨╗?  " + c
ompareNumbers(2));
                                                              ^
6 errors


And here's my code:



I think that this error can be caused wrong spelling, wrong case(which I checked twice).
Also error may be because of player[p] is created in another createPlayers() method.
But if I create player[p] like this outside of method:
...
randomNum TheNumber = new randomNum();

player[0] = new playerStuff();
player[1] = new playerStuff();
player[2] = new playerStuff();

public boolean compareNumbers(int x) {
...

then I will get <identifier> expected error which means that I didn't put that code inside of method.

So how do I create player[p] outside of main method and use it inside main method? Or I have to create them inside main method? Or maybe there is another problem that I don't see?

 
Tim Moores
Saloon Keeper
Posts: 3885
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some of the errors result from methods being in a different class than from where you are using them. E.g., both createPlayers and compareNumbers are part of class "playerStuff", but you call them in class GuessGame - that won't work. My guess is that the method createPlayers should really be a constructor of class playerStuff. If you store that in a variable (say, called "players") in class GuessGame, you could later call the players.compareNumbers(0).

"TheNumber" is simply not declared - it seems that it should be an object of type "randomNum", and the method you would call on it should be called "setRandomNum", not "setNum".

Lastly, you should get in the habit of starting class names with uppercase letters - that's what the rest of the Java world does, and expects from code they read. So, "PlayerStuff" and "RandomNum" instead of what you have now.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Cannot find symbol" means that you are using a name in the program and the compiler doesn't know what the name means - as far as the compiler is concerned there is no thing with that name in the specified context.

For example, look at what the compiler is telling you here:

GuessGame.java:51: cannot find symbol
symbol  : method createPlayers()
location: class GuessGame
                createPlayers();
                ^

It is telling you that there is no method named createdPlayers in class GuessGame.

And if I look at your code, I see that there is indeed no such method in class GuessGame. There is in class playerStuff, but you are trying to call this method in the wrong context. You have to call that method on a playerStuff object.

It looks like you don't fully understand yet what it means if you put methods and member variables in a class. You can call those methods and access those member variables only through an instance (= an object) of that class.
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't call methods on objects because these methods were in another class(castle)? As I understood, I have to use variables in the same method where they have been created.
So because of that, I put all variables in one methods and I interacted with them inside the same method.



And that is working properly.
I had to delete all the methods and "while" cycles because I couldn't access "player" variable or "TheNumber" variable in there. Is there a way to make some kind of automation like I tried through methods and cycles? Maybe I didn't learn Java far enough, but I'm a little bit concerned about inaccessible variables between different methods.

Tim Moores wrote:... Lastly, you should get in the habit of starting class names with uppercase letters - that's what the rest of the Java world does, and expects from code they read. So, "PlayerStuff" and "RandomNum" instead of what you have now.
Is there a special place, where I can read about these rules? I'm just trying to make my code as much understandable as I can, so I and anybody else could read my code easily.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rich McStone wrote:I couldn't call methods on objects because these methods were in another class(castle)?


Not true. Of course, you can call methods using object... provided that the methods are accessible, and you use the instance to call them.

Rich McStone wrote:As I understood, I have to use variables in the same method where they have been created.


This is only true for local variable. This is not true for other types of variables (such as instance and static variables), which operate at a different scope.

Rich McStone wrote:So because of that, I put all variables in one methods and I interacted with them inside the same method.


Putting everything in one method, is really *not* a good idea... which many will point out to you soon.

Henry
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: This is only true for local variable. This is not true for other types of variables (such as instance and static variables), which operate at a different scope.  

I guess I haven't got to that part yet. I didn't learn about other types of variables.

Rich McStone wrote: I couldn't call methods on objects because these methods were in another class(castle)?

I must have been very tired yesterday  . I already called methods from different classes in my code. I pretty much knew about that.

Henry Wong wrote: Putting everything in one method, is really *not* a good idea... which many will point out to you soon. 

I suspected that was not a proper way of coding in an object-oriented language.

I probably shouldn't worry about using objects in different methods right now. I will learn about stronger coding in near future.

Thanks for your help guys! I really appreciate your helping to beginners I don't know what I would do without this forum.




 
Tim Moores
Saloon Keeper
Posts: 3885
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a special place, where I can read about these rules?

While http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html is labelled as no longer maintained, it is still a good starting point. The general rules for naming and organizing things have not really changed. Google also has some ideas on that: https://google.github.io/styleguide/javaguide.html
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote: While http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html is labelled as no longer maintained, it is still a good starting point. The general rules for naming and organizing things have not really changed. Google also has some ideas on that: https://google.github.io/styleguide/javaguide.html

Thanks a lot! I almost forgot about that.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rich McStone wrote:
Henry Wong wrote: This is only true for local variable. This is not true for other types of variables (such as instance and static variables), which operate at a different scope.  

I guess I haven't got to that part yet. I didn't learn about other types of variables.


Of the three classes that you posted, in the original post, two of those classes have instance variable declarations. You even declared them with access modifiers, which local variables don't have.

Henry
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I knew that indeed. I had some problems with translations, so I was confused about instance variables. I didn't understand what was it at first, but now I figured everything out, and I made program the way I wanted it at first.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!