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 errors

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot find symbol errors

    Getting "cannot find symbol" errors at 66 and 116. I don't see any spelling errors and the variables are declared, so not sure why I'm getting the errors.

    This program prints out: current account balance, balance after making a deposit, balance after making a withdraw and balance after calculating the interest.

     
    class Account {
     
    //Declare the variables
     
    	private String name;
    	private double deposit;
    	private double withdraw;
    	private double balance;
    	private double intrate;
     
    //Declare getter and setter methods
     
    	public String getName()			{
    		return name;
    	}
     
    	public double getDeposit()		{
    		return deposit;
    	}
     
    	public double getWithraw()		{
    		return withdraw;							
    	}
     
    	public double getBalance()		{
    		return balance;
    	}
     
    	public double getIntrate()		{
    		return intrate;
    	}
     
    	public void setName(String n)		{
    		name = n;
    	}
     
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
     
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
     
    	public void setBalance(double b)	{
    		balance = b;
    	}
     
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
     
     
    //Calculate the balance after the deposit.
     
    	public double BalAfterDeposit()	{
    		double balance = balance + deposit;
    		return balance;
    	}
     
    //Calculate the balance after the withdraw.
     
    	public double BalAfterWithdraw() {
    		double balance = balance - withdraw;
    		if (balance < withdraw)	{
    	          System.println("Error. Balance not enough to cover withdraw.");
    		}
     
    		else {
    			return balance;
    		}
    	}
     
    }
     
    	class AccountTestDrive	{
     
    	public static void main (String[] args)	{
     
    	//Create an array that holds 2 new accounts
     
    		Account[] owner;
    		owner = new Account[2];
     
    		owner[0] = new Account();
     
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
     
    			System.out.println("Account Owner1 Name:" + 			
     
    			owner[0].getName());
     
    			System.out.println("Account Owner1 Current Balance:" + 		
     
    			owner[0].getBalance());
     
    			System.out.println("Account Owner1 Balance after Deposit:" 	
     
    			+ owner[0].BalAfterDeposit());
     
    			System.out.println("Account Owner1 Balance after Withdraw:" 
     
    			+ owner[0].BalAfterWithdraw());
     
     
    		owner[1] = new Account();
     
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
     
    			System.out.println("Account Owner2 Name:" + 			
     
    			owner[1].getName());
     
    			System.out.println("Account Owner2 Current Balance:" + 		
     
    			owner[1].getBalance());
     
    			System.out.println("Account Owner2 Balance after Deposit:" 	
     
    			+ owner[1].BalAfterDeposit());
     
    			System.out.println("Account Owner2 Balance after Withdraw:" 
     
    			+ owner[1].BalAfterWithdraw());
     
    			System.out.println("Account Owner2 Balance with Interest:" 	
     
    			+ owner[1].add_monthly_interest());
    	}
      }
    	//Class SavingsAccount inherits methods and instances from Account
     
    		class SavingsAccount extends Account	{
     
    			double intrate = 0.05;
     
    			public double add_monthly_interest() {
    			 double balance = balance + (balance * intrate/12);
    			 return balance;
    			}
     
    		}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,445
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Cannot find symbol errors

    What exact lines are the errors on? I can't count that high...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol errors

     
    System.println("Error. Balance not enough to cover withdraw.");


    and


     
     
    System.out.println("Account Owner2 Balance with Interest:" + owner[1].add_monthly_interest());

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Cannot find symbol errors

    While not knowing the exact lines you are having errors, I can tell you that the Account class doesn't have an add_monthly_interest method, so you cannot invoke that method on an Account object. You did create a class that extends Account (SavingsAccount) which has that method, but the Account class itself does not. When you created your second Account in your main, you attempt to call the add_monthly_interest() method on it. That should throw a compile error. Even if you did create the second Account as a SavingsAccount in your main, you cannot call that method from a generic Account object (which is what you get from owner[1] since your array contains Account objects), since that method doesn't exist for an Account object.
    Also, you are creating a second intrate variable in your SavingsAccount class. I'm not sure of the exact effect of doing this (and I can't be bothered writing up a test program to see), but I would guess that your add_monthly_interest() method would use the intrate variable you created in the SavingsAccount class, while every method you inherited from the Account class will use Account's private intrate variable. If your intention is to initiate the intrate variable, you need to do so in a constructor. Also, since the intrate variable in Account is "private", you will have to either use the setters and getters to manipulate the value in the SavingsAccount class, or make the intrate variable "protected" (or "public" if you don't care about scope). Remember: "private" variables and methods are NOT accessible from a child class. They are still inherited, but they are out of the scope of the child class. "Protected" and "Public" variables and methods are accessible from a child class.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol errors

    I'm able to compile and run the program but I think it runs for the wrong reasons. The add_monthly_interest() method for calculating the balance with interest was suppose to take place in the SavingsAccount class. I tried to make add_monthly_interest() an abstract method in the Account class and then put the code for calculating the balance with interest in the add_monthly_interest() method in the SavingsAccount class but I got errors.

    Now, the SavingsAccount class is abstract as is the add_monthly_interest() method in the SavingsAccount class but that's not what I wanted, eventhough the program compiles.

     
    class Account {
     
    //Declare the variables
     
    	private String name;
    	protected double balance;
    	private double deposit;	
    	private double withdraw;
    	protected double intrate;
     
    //Declare getter and setter methods
     
    	public String getName()			{
    		return name;
    	}
     
    	public double getDeposit()		{
    		return deposit;
    	}
     
    	public double getWithraw()		{
    		return withdraw;							
    	}
     
    	public double getBalance()		{
    		return balance;
    	}
     
    	public double getIntrate()		{
    		return intrate;
    	}
     
    	public void setName(String n)		{
    		name = n;
    	}
     
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
     
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
     
    	public void setBalance(double b)	{
    		balance = b;
    	}
     
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
     
     
    //Calculate the balance after the deposit.
     
    	public double BalAfterDeposit()	{
    		double total = balance + deposit;
    		return total;
    	}
     
    //Calculate the balance after the withdraw.
     
    	public double BalAfterWithdraw() {
     
     
    		double total = balance - withdraw;
    		return total;
     
    	}
     
     
    	public double add_monthly_interest()	{
    		double intrate = 0.05;
    		double total = balance + (balance * intrate)/12;
    		return total;
    	}
     
     
    }
     
    	class AccountTestDrive	{
     
    	public static void main (String[] args)	{
     
    	//Create an array that holds 2 new accounts
     
    		Account[] owner;
    		owner = new Account[2];
     
    		owner[0] = new Account();
     
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
     
    			System.out.println("Account Owner1 Name:" + 			
     
    			owner[0].getName());
     
    			System.out.println("Account Owner1 Current Balance:" + 		
     
    			owner[0].getBalance());
     
    			System.out.println("Account Owner1 Balance after Deposit:" 	
     
    			+ owner[0].BalAfterDeposit());
     
    			System.out.println("Account Owner1 Balance after Withdraw:" 
     
    			+ owner[0].BalAfterWithdraw());
     
     
     
     
    		owner[1] = new Account();
     
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
     
    			System.out.println("Account Owner2 Name:" + 			
     
    			owner[1].getName());
     
    			System.out.println("Account Owner2 Current Balance:" + 		
     
    			owner[1].getBalance());
     
    			System.out.println("Account Owner2 Balance after Deposit:" 	
     
    			+ owner[1].BalAfterDeposit());
     
    			System.out.println("Account Owner2 Balance after Withdraw:" 
     
    			+ owner[1].BalAfterWithdraw());
     
    			System.out.println("Account Owner2 Balance with interest:" 	
     
    			+ owner[1].add_monthly_interest());
     
     
     
     
    	}
      }
     
    	abstract class SavingsAccount extends Account	{
    		public abstract double add_monthly_interest();
     
    	}

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Cannot find symbol errors

    SavingsAccount probably shouldn't be abstract. You cannot initialize abstract classes. If anything, Account should be abstract and should contain an abstract add_monthly_interest() method. But, that would mean that you could not initialize an Account object (although you could initialize all of your accounts as SavingsAccounts, if that's what you wanted). If you don't want Account to be abstract, the best I can suggest is to just include an add_monthly_interest() method that just doesn't do anything (perhaps returns 0 or something). You can then override that method in your SavingsAccount class, when you inherit the Account class, and provide SavingsAccount with the implementation that you want.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,445
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Cannot find symbol errors

    Quote Originally Posted by UM68 View Post
     
    System.println("Error. Balance not enough to cover withdraw.");
    Look really carefully at this line. Where in the API does it show that the System class has a println() function?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol errors

    I tried putting a add_monthly_interest() method in the Account class. Declared it public so it could be inherited by the SavingsAccount class. I tried to make it so the method does nothing in the Account class then could be overridden in the SavingsAccount class. I tried return null but get an error saying "incompatible types".

    If I make the add_monthly_interest() method in Account of type void then of course I get an error saying the add_monthly_interest() method in SavingsAccount cannot override since double is not compatible with void.

    Here's my updated code:

     
    class Account {
     
    //Declare the variables
     
    	private String name;
    	protected double balance;
    	private double deposit;	
    	private double withdraw;
    	protected double intrate;
     
    //Declare getter and setter methods
     
    	public String getName()			{
    		return name;
    	}
     
    	public double getDeposit()		{
    		return deposit;
    	}
     
    	public double getWithraw()		{
    		return withdraw;							
    	}
     
    	public double getBalance()		{
    		return balance;
    	}
     
    	public double getIntrate()		{
    		return intrate;
    	}
     
    	public void setName(String n)		{
    		name = n;
    	}
     
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
     
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
     
    	public void setBalance(double b)	{
    		balance = b;
    	}
     
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
     
     
    //Calculate the balance after the deposit.
     
    	public double BalAfterDeposit()	{
    		double total = balance + deposit;
    		return total;
    	}
     
    //Calculate the balance after the withdraw.
     
    	public double BalAfterWithdraw() {
     
     
    		double total = balance - withdraw;
    		return total;
     
    	}
     
    	public double add_monthly_interest()	{
    		return null;	
    	}
     
     
     
    }
     
     
     
     
    	class AccountTestDrive	{
     
    	public static void main (String[] args)	{
     
    	//Create an array that holds 2 new accounts
     
    		Account[] owner;
    		owner = new Account[2];
     
    		owner[0] = new Account();
     
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
     
    			System.out.println("Account Owner1 Name:" + 			
     
    			owner[0].getName());
     
    			System.out.println("Account Owner1 Current Balance:" + 		
     
    			owner[0].getBalance());
     
    			System.out.println("Account Owner1 Balance after Deposit:" 	
     
    			+ owner[0].BalAfterDeposit());
     
    			System.out.println("Account Owner1 Balance after Withdraw:" 
     
    			+ owner[0].BalAfterWithdraw());
     
     
     
     
    		owner[1] = new Account();
     
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
     
    			System.out.println("Account Owner2 Name:" + 			
     
    			owner[1].getName());
     
    			System.out.println("Account Owner2 Current Balance:" + 		
     
    			owner[1].getBalance());
     
    			System.out.println("Account Owner2 Balance after Deposit:" 	
     
    			+ owner[1].BalAfterDeposit());
     
    			System.out.println("Account Owner2 Balance after Withdraw:" 
     
    			+ owner[1].BalAfterWithdraw());
     
    			System.out.println("Account Owner2 Balance with interest:" 	
     
    			+ owner[1].add_monthly_interest());
     
     
     
     
    	}
      }
     
    	class SavingsAccount extends Account	{
    	 public double add_monthly_interest() {
    		double intrate = 0.05;
    		double total = balance + (balance * intrate)/12;
    		return total;
    	   }
     
     
    	}

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Cannot find symbol errors

    "Null" is only valid for object. double is not an object, it is a primitive. The "default" value for a double is 0. I suppose if you really really wanted to return null, you could turn the return type into a Double object (notice upper case instead of lowercase). Depending on your IDE, you may get some boxing warnings, but it will compile.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol errors

    Yes, it did compile when the following statement was placed in the Account class:

     
    System.out.println("Account Owner2 Balance with interest:" + 		
     
    owner[1].add_monthly_interest());

    However, the output was "Account Owner2 balance with interest: 0"

    The add_monthly_interest() method in SavingAccount didn't override the same method in Account class.

    So then I moved the
    System.out.println("Account Owner2 Balance with interest:" + 		
     
    		owner[1].add_monthly_interest());
    statement down into the SavingsAccount class and get a bunch of errors: ; expected, ) expected, and illegal start of type and identifier expected.

    They all reference that one statement.

    Updated code:

     
    class Account {
     
    //Declare the variables
     
    	private String name;
    	protected double balance;
    	private double deposit;	
    	private double withdraw;
    	protected double intrate;
     
    //Declare getter and setter methods
     
    	public String getName()			{
    		return name;
    	}
     
    	public double getDeposit()		{
    		return deposit;
    	}
     
    	public double getWithraw()		{
    		return withdraw;							
    	}
     
    	public double getBalance()		{
    		return balance;
    	}
     
    	public double getIntrate()		{
    		return intrate;
    	}
     
    	public void setName(String n)		{
    		name = n;
    	}
     
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
     
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
     
    	public void setBalance(double b)	{
    		balance = b;
    	}
     
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
     
     
    //Calculate the balance after the deposit.
     
    	public double BalAfterDeposit()	{
    		double total = balance + deposit;
    		return total;
    	}
     
    //Calculate the balance after the withdraw.
     
    	public double BalAfterWithdraw() {
     
     
    		double total = balance - withdraw;
    		return total;
     
    	}
     
    	public double add_monthly_interest()	{
    		return 0;	
    	}
     
     
     
    }
     
     
     
     
    	class AccountTestDrive	{
     
    	public static void main (String[] args)	{
     
    	//Create an array that holds 2 new accounts
     
    		Account[] owner;
    		owner = new Account[2];
     
    		owner[0] = new Account();
     
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
     
    			System.out.println("Account Owner1 Name:" + 			
     
    			owner[0].getName());
     
    			System.out.println("Account Owner1 Current Balance:" + 		
     
    			owner[0].getBalance());
     
    			System.out.println("Account Owner1 Balance after Deposit:" 	
     
    			+ owner[0].BalAfterDeposit());
     
    			System.out.println("Account Owner1 Balance after Withdraw:" 
     
    			+ owner[0].BalAfterWithdraw());
     
     
     
     
    		owner[1] = new Account();
     
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
     
    			System.out.println("Account Owner2 Name:" + 			
     
    			owner[1].getName());
     
    			System.out.println("Account Owner2 Current Balance:" + 		
     
    			owner[1].getBalance());
     
    			System.out.println("Account Owner2 Balance after Deposit:" 	
     
    			+ owner[1].BalAfterDeposit());
     
    			System.out.println("Account Owner2 Balance after Withdraw:" 
     
    			+ owner[1].BalAfterWithdraw());
     
     
     
     
     
     
    	}
      }
     
    	class SavingsAccount extends Account	{
    	 public double add_monthly_interest() {
    		double intrate = 0.05;
    		double total = balance + (balance * intrate)/12;
    		return total;
    	   }
    		System.out.println("Account Owner2 Balance with interest:" + 		
     
    		owner[1].add_monthly_interest()); 			        
     
    	}

Similar Threads

  1. [SOLVED] cannot find symbol
    By ATIBA SHEIKH in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 26th, 2012, 02:28 AM
  2. [SOLVED] Cannot Find Symbol
    By ChicoTheMan94 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2012, 02:46 PM
  3. Remaining compile errors: no suitable method found for & cannot find symbol
    By ChuckLep in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2011, 02:33 PM
  4. Cannot Find Variable/Symbol Errors... why?
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 9th, 2011, 04:12 AM
  5. Help with Cannot Find Symbol Variable errors
    By skboone in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 10:52 AM