File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java:12: cannot find symbol Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java:12: cannot find symbol" Watch "java:12: cannot find symbol" New topic
Author

java:12: cannot find symbol

Niall Doyle
Greenhorn

Joined: Jul 14, 2009
Posts: 7
Hi guys, Im new to programming in general...thanks in advance for your help:

Im using jcreator to compile the following file (Employee.java which I have saved in C:\JavaPrograms):




The errors I am getting are as follows:

C:\JavaPrograms\Employee.java:12: cannot find symbol
symbol : constructor Employee(java.lang.String,java.lang.String,int,double)
location: class Employee
Employee emp = new Employee("John Woo", "Java Programmer", 2125556347, 20000.00);
^
C:\JavaPrograms\Employee.java:14: cannot find symbol
symbol : method printEmpInfo()
location: class Employee
emp.printEmpInfo();


When I run it, should it create an Employee.class file in the same folder? Because this is not happening..any ideas? thanks!
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Niall,

Welcome to JavaRanch! Your first error is pretty simple. You'll want to read up on constructors and object creation. The second error is also pretty obvious. Can you find a method called printEmpInfo() anywhere in your class definition?

John.
Niall Doyle
Greenhorn

Joined: Jul 14, 2009
Posts: 7
Hi John thanks a lot for your help so far...
I read a bit about Constructors and I added in the line: public Employee() { in the class definition
So heres what I have now:



It still has not created my Employee.class file?? and the new errors are as follows:

C:\JavaPrograms\Employee.java:13: illegal start of expression
public static void main(String args[]) {
^
C:\JavaPrograms\Employee.java:13: illegal start of expression
public static void main(String args[]) {
^
C:\JavaPrograms\Employee.java:13: ';' expected
public static void main(String args[]) {
^
C:\JavaPrograms\Employee.java:13: ')' expected
public static void main(String args[]) {
^
C:\JavaPrograms\Employee.java:13: illegal start of expression
public static void main(String args[]) {
^
C:\JavaPrograms\Employee.java:13: ';' expected
public static void main(String args[]) {
^
C:\JavaPrograms\Employee.java:13: illegal start of expression
public static void main(String args[]) {
^
C:\JavaPrograms\Employee.java:13: ';' expected
public static void main(String args[]) {
^
C:\JavaPrograms\Employee.java:23: class, interface, or enum expected
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61


The constructor you have should have a closing block "}", but the program will produce an error when compiled. The constructor that you use in instantiation will not work because you don't have any constructor that have those parameters/arguments. like this

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Welcome to JavaRanch. You have already had useful replies. Please use the code button; I have edited your earlier posts so you can see how much easier they are to read. You need to be careful about indentation. Many of us prefer BSD Allman indentation (as described here) to the form you are using. But whichever convention you use, you must stick to it strictly; then errors like not ending a method with enough } will become obvious.

And please read the important administrative private message Rob sent you earlier.
Niall Doyle
Greenhorn

Joined: Jul 14, 2009
Posts: 7
OK guys thanks for your patience, like I said Im new to this (including indentation ) but I am picking up more as I go along..




OK the error Im now getting is:

C:\JavaPrograms\Employee.java:17: invalid method declaration; return type required
public printEmpInfo() {

I hope Im not going backwards
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61


you could put void as a return type if you don't want the method to return any value



or you could put String as a return type.

you should specify a return type for the method printEmpInfo() - I put void you could also use the reference type String if you want the method to just return a string. remember the syntax in declaring a method.



Happy coding...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
It may seem silly to go on about indentation, but it does make it easier to find certain irritating errors. You can get no end of trouble if you write "public" inside a method or don't get your {} correctly balanced. Both become easier to finf if you get your indentation right.

You have a problem in the constructor you have posted. Try it withand see what happens. Have a look at this Java™ Tutorials page. Note that the way you wrote your constructor parameters, you will have to writenotNote you will have problems with phone numbers; look very very carefully at the way you spelt the phone number variables in different places.
Niall Doyle
Greenhorn

Joined: Jul 14, 2009
Posts: 7
OK seems to be working ok now but I feel Im not quite going about it the correct way...




Campbell, I appreciate all the tips you have given me including the indentation...I know I have a lot to learn thanks again!

However when I try
in place of


I get the error:
new Employee("Campbell Ritchie", "Bartender", 2345678, 123456.789);
^
C:\JavaPrograms\Employee.java:11: ';' expected
new Employee("Campbell Ritchie", "Bartender", 2345678, 123456.789);
^
C:\JavaPrograms\Employee.java:11: illegal start of type
new Employee("Campbell Ritchie", "Bartender", 2345678, 123456.789);
^
C:\JavaPrograms\Employee.java:11: <identifier> expected
new Employee("Campbell Ritchie", "Bartender", 2345678, 123456.789);

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Niall,

You shouldn't have a semicolon on your method declaration line. BTW, you probably should change your constructor to look like this:

this makes it more generally usable.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You want that line as well as the heading for the constructor.

That line is part of a statement. Put it all on its ownAnd see what happens, assuming you have your printEmpInfo method working.
Niall Doyle
Greenhorn

Joined: Jul 14, 2009
Posts: 7
OK thanks guys! heres where I am now..



Latest error is:
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Niall,

Did you read my post? You really should change your constructor to match what I did. That will fix those error messages.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Afraid not.

You have a constructor and you pass information to it.Here we have a class with two fields. One of them, how much water the glass contains, called content, is set to 0 automatically, so you always end up with your glass empty. Not half-empty, empty. So you can fill it or empty it, with the two methods. Note the second bit of code, which can fit inside any method, here in a main method, creates two glasses, size 10 and 15.

You take some code, then you pass information from that code in () after "Glass". So that information goes to the Glass constructor. You can see it arrives as 10 or 15. Then you pass it and you want to get a glass that size, 10 or 15. Apart from the ??? that Glass class will compile, and the second code will run. And this is the output
java$ java Glass
A glass size 10 containing 10 of water.
A glass size 10 containing 0 of water.
A glass size 15 containing 15 of water.
A glass size 15 containing 0 of water.
Going back nearly 24 hours in this thread, looking at the very heavy hints people have dropped, and knowing that Glass class will compile if you make one correction, this is the $64 question:


What can you replace the ??? with to make it work?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
John de Michele wrote:Niall,

Did you read my post? You really should change your constructor to match what I did. That will fix those error messages.

John.
If you haven't worked out where the "heavy hints" were, that post should make them easier to find.
Niall Doyle
Greenhorn

Joined: Jul 14, 2009
Posts: 7
Ah ok..gotcha...I didnt realise "this" was a special variable in java, thanks again!!

"The variable, this, is used in the source code of an instance method to refer to the object that contains the method. This intent of the name, this, is to refer to "this object," the one right here that this very method is in. If x is an instance variable in the same object, then this.x can be used as a full name for that variable. If otherMethod() is an instance method in the same object, then this.otherMethod() could be used to call that method. Whenever the computer executes an instance method, it automatically sets the variable, this, to refer to the object that contains the method."


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You have got the constructor looking correct. And I presume you have worked out what to replace the ??? with in my Glass class. But work out what will happen if you try this codeYou create an Employee called John Woo, then see what it prints out . . .

Do you see the problem in your printEmpInfo method? Any idea how to sort it out?

Hint: The solutions to all these problems are similar to one another.
Niall Doyle
Greenhorn

Joined: Jul 14, 2009
Posts: 7
Yes..OK i see what you mean now i can add new Employees as I go along because the Constructor acts as containers for name, role etc...thanks for your patience again!

Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Now you got all working fine, that's good for you. I guess the phone number data type should be changed to a string or a long data type.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Brilliant.

I think it would be better to say the constructor is a recipient for the incoming data, and the object itself acts as their container.
You have got rid of the real problems. A few System.out.print(" "); calls will be necessary in the printing method, otherwise your output will run together into a single block of text. Or have a look at this Java™ Tutorials page, then you can reduce your printInfo method to this:That should work if you don't change the types of the fields. The meanings of the % tags are in the Java Tutorials page, or the links it contains, or here but it is not easy reading.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java:12: cannot find symbol