Home > front end >  I can't seem to find the reason why its "error: cannot find symbol"
I can't seem to find the reason why its "error: cannot find symbol"

Time:12-16

[Disclaimer: Beginner in Java]

My program has run into a problem just now, while I was trying to finish it up. The error was that it cannot find the symbol, and the IDE (I am using Text Pad) pinpoints it inside the main method, check below:

BalanceW.java:22: error: cannot find symbol
        account.withdraw(500.00);
               ^
  symbol:   method withdraw(double)
  location: variable account of type Account

If you wanna know the code, here you go:

Account.java

public class Account{
    private String accntNumber;
    private String accntName;
    private double balance;

    public Account(){}
    public Account(String num, String name, double bal){
        accntNumber = num;
        accntName = name;
        balance = bal;
    }

    public double getBalance(){ return balance;}
}

BalanceW.java (I condensed the main method inside the Balance W so this post won't be too long)

public class BalanceW extends Account{

    public double withdraw(double amount){
        double bal = getBalance();
            if(amount <= 0){
                throw new ArithmeticException("Invalid amount: Amount is less than 0");
            }
            if(amount > bal){
                throw new ArithmeticException("Insufficient: Insufficient funds");
            }
            bal = bal - amount;
            return amount;

    }
        public static void main(String[] args){
        Account account = new Account("Acct-001","Juan dela Cruz", 5000.0);
        account.withdraw(500.00);
        System.out.println("Balance: " account.getBalance());
    }
}

I just wanna know how, and why it went wrong. To be fair, I have looked up multiple tabs finding ways to fix the error. Thank you very much for reading through the post, and I really appreciate it if you can help in this problem I waltz into.

CodePudding user response:

The method withdraw is defined for BalanceW, not for Account. If you're going to use this method, in the first line of method main you need to declare account as BalanceW as below:

BalanceW account = new BalanceW("Acct-001", "Juan dela Cruz", 5000.0);
account.withdraw(500.00);
  • Related