Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Cannot find symbol error

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Cannot find symbol error

    Hello All,

    Am hoping someone can tell me what the problem with the following code is:

    The following class is saved as D:\Java\Account.java
    public class Account{
     
    	public double balance;
     
    	public Account(double initBalance){
    		balance = initBalance;
    	}
    }

    The following class is saved as D:\Java\AccountTest.java

    public class AccountTest{
     
    	public static void main(String[] args){
     
    		Account acct = new Account(100.0);
    		acct.balance = acct.balance + 47.0;
    		acct.balance = acct.balance - 150.0;
     
    		System.out.println("final account balance is: "+acct.balance);		
     
    	}
    }
    For some reason, Account.java compiles correctly, but AccountTest does not and gives me a cannot find symbol error stating that it cannot find the Account class. I have tried this several times over and I get the same problem. I tried compiling AccountTest directly, since it should compile Account automatically, but it didn't, so I tried compiling them separately and get the problem mentioned above. I hope someone can help me with this! Thanks in advance!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Cannot find symbol error

    Hmmmm...not sure why it couldn't find Account class itself. Can say that you should have something like

     
    public class Account{
     
    private double balance;
     
    public Account(double initBalance){
    setBalance(initBalance);
    }
     
    public void setBalance(double initBalance)
    {
    this.balance = initBalance;
    }
     
    public double getBalance()
    {
    return balance;
    }
     
    }

     
    public class AccountTest{
     
    public static void main(String[] args){
     
    Account acct = new Account(100.0);
    acct.setBalance(acct.getBalance()+ 47.0) ;
    acct.setBalance(acct.getBalance() - 150.0) ;
     
    System.out.println("final account balance is: "+acct.getBalance());
     
    }
    }

    See if the problem still is there.

    If it still is mad, put both classes in the same package.

    If it still won't work after that, I don't know what to say.

    It works now.
    Last edited by javapenguin; February 15th, 2011 at 05:20 PM. Reason: Fixing erros made due to sleepiness.

  3. #3
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Cannot find symbol error

    javapenguin: You posted code that doesn't compile. Why do you insist on continuing to do this?

    OP: The code you posted compiles just fine for me. Compiling AccountTest.java alone should do all the required work. Not sure what your problem is. I copied and pasted the code you posted into two files in the same dir, and they compiled just fine. Obviously this isn't that helpful, since it isn't working for you, but we probably need more info. Post the commands you are running to compile (along with the prompt that shows what directory you are running these from), as well as a directory listing.

  4. The Following User Says Thank You to DavidFongs For This Useful Post:

    AnuR (February 23rd, 2011)

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Exclamation Re: Cannot find symbol error

    Quote Originally Posted by DavidFongs View Post
    javapenguin: You posted code that doesn't compile. Why do you insist on continuing to do this?

    OP: The code you posted compiles just fine for me. Compiling AccountTest.java alone should do all the required work. Not sure what your problem is. I copied and pasted the code you posted into two files in the same dir, and they compiled just fine. Obviously this isn't that helpful, since it isn't working for you, but we probably need more info. Post the commands you are running to compile (along with the prompt that shows what directory you are running these from), as well as a directory listing.
    It doesn't?

    Let me check it out.



    I put doube instead of double once.

    And I forgot to put a value in the set Balance. That was probably a result of being very tired and frustrated from doing a lot of homework.

    Mistakes happen.

    Have corrected it.

    public class Account{
     
    private double balance;
     
    public Account(double initBalance){
    setBalance(initBalance);
    }
     
    public void setBalance(double initBalance)
    {
    this.balance = initBalance;
    }
     
    public double getBalance()
    {
    return balance;
    }
     
    }

     
    public class AccountTest{
     
    public static void main(String[] args){
     
    Account acct = new Account(100.0);
    acct.setBalance(acct.getBalance()+ 47.0);
    acct.setBalance(acct.getBalance() - 150.0) ;
     
    System.out.println("final account balance is: "+acct.getBalance());
     
    }
    }

     
     
    final account balance is: -3.0


    Last edited by javapenguin; February 15th, 2011 at 05:08 PM. Reason: Correcting errors made earlier

  6. The Following User Says Thank You to javapenguin For This Useful Post:

    AnuR (February 23rd, 2011)

  7. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Red face Re: Cannot find symbol error

    Quote Originally Posted by DavidFongs View Post
    javapenguin: You posted code that doesn't compile. Why do you insist on continuing to do this?

    OP: The code you posted compiles just fine for me. Compiling AccountTest.java alone should do all the required work. Not sure what your problem is. I copied and pasted the code you posted into two files in the same dir, and they compiled just fine. Obviously this isn't that helpful, since it isn't working for you, but we probably need more info. Post the commands you are running to compile (along with the prompt that shows what directory you are running these from), as well as a directory listing.
    As the OP does appear to have the directories.

    It says they're both on the D Drive and should be able to be seen by each other.

    I had recommended putting both in same package if it still wouldn't compile.

  8. The Following User Says Thank You to javapenguin For This Useful Post:

    AnuR (February 23rd, 2011)

  9. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Cannot find symbol error

    Please test any code before posting it as a solution. Incorrect code leads to more confusion!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  10. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Cannot find symbol error

    Quote Originally Posted by javapenguin View Post
    It doesn't?

    Let me check it out.

    I put doube instead of double once.

    And I forgot to put a value in the set Balance. That was probably a result of being very tired and frustrated from doing a lot of homework.

    Mistakes happen.

    Have corrected it.
    That, and you also had assignment errors.

    While you are right that mistakes do happen, they don't happen if you TRY it first. Which is incredibly simple to do. Instead, by being lazy, you are posting code that likely leads to more confusion than it does good for the OP. Weren't you banned for this once already? You would think you would learn your lesson. In this case it is as easy as saving the two classes, and typing "javac AccountTest.java"

  11. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol error

    Thanks to everyone for trying to help me with this!! But I found out that the problem was not in my code, but in my classpath setting for some weird reason!! Originally my classpath was as follows:
    Classpath = C:\Program Files\Java\jdk1.6.0_23\bin;
    Now I've set it to:
    Classpath = .;C:\Program Files\Java\jdk1.6.0_23\bin;

    Am not sure why I need the ".;" in front of the path itself, I did it only cos I was told to and it solved my problem! Can some one explain why that works? Am not sure if I making my question clear, please feel free to ask me to elaborate further.

    Thanks once again!!!

    Anu

  12. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Cannot find symbol error

    the .; tells the java compiler to also search in the current directory (actually, only the . tells it to search in the current directory, the ; allows you to let it search in multiple directories separated by ;).

  13. The Following User Says Thank You to helloworld922 For This Useful Post:

    AnuR (February 23rd, 2011)

  14. #10
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol error

    Thanks a lot!! Once again to everyone for helping me with this and explaining the classpath issue to me.

    Anu

Similar Threads

  1. Cannot find symbol?
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 10th, 2010, 10:02 PM
  2. cannot find symbol Error
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2010, 02:36 AM
  3. Why the compiler can not find the symbol?
    By AlicNewbie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 08:16 PM
  4. cannot find symbol - method
    By kyuss in forum Object Oriented Programming
    Replies: 2
    Last Post: December 7th, 2009, 01:01 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM

Tags for this Thread