• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

cannot find symbol error

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
"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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic