Home > Mobile >  Why am I getting this error unexpected type required: variable, found: value in my method?
Why am I getting this error unexpected type required: variable, found: value in my method?

Time:04-22

I am trying to update my account balance. I have a getAccountBalance accessor in my Account class in the same file as my main method, my main method has the Account object created. The only information that is important is below, the rest isn't necessary. It's a school assignment. I have to use a withdraw method separate from the Account class, otherwise I would of easily just create a withdraw method in the Account class.

    public static void withdrawFromAccount(double amount ,Account a){
      a.getAccountBalance()-=amount; // unexpected error type required: variable found: value
    }

CodePudding user response:

Picked up from this :

Required: Variable Found: Value

=> in your case a.getAccountBalance() returns a value, not a variable. You are currently trying to assign a value to a value... For getting the var, you are using a getter, for setting it, use a setter... I'll let you find the rest of it ;)

CodePudding user response:

You are trying to assign a value to another value. This is akin to saying 7 = 1 2. That's not going to work. Instead, do the calculation and then assign that via a setter.

public static boolean withdrawFromAccount(double amount, Account account) {
  account.setAccountBalance(account.getAccountBalance() - amount);
}

Also, consider who is responsible for error checking. Do you need to validate sufficient funds? What about negative inputs?

  •  Tags:  
  • java
  • Related