David
David

Reputation: 1

Calling a method, but won't return the values. Java

So I have an assignment where I have to make a bank account. Long story short I have a class that has your actions (deposit, withdraw etc) in methods, and a Driver which would fills in the blanks (i.e client name, balance etc.) as well it calls the methods from the other class.

Now my issue. For an example, when I deposit, my variables send fine to the method and do the math correctly and have the correct balance, but returning the balance's new value is an issue. It is still the same value it was before in the driver.

This is my driver's deposit code

 String deposit1 = JOptionPane.showInputDialog ("How much would you like to deposit?");
 int deposit = Integer.parseInt (deposit1);
 myBank.getDeposit(deposit, balance);

This is my deposit method code

 public void getDeposit(int deposit, int balance)
{
    if (deposit<0)
    {
        System.out.println ("Invalid Number");
        deposit=0;
    }
    balance =balance + deposit;
}

Any help would be greatly appreciated, can't seem to find any solutions and I've been stuck for some time

Thanks

P.S This is my first time on these forums so sorry if I did anything wrong

P.P.S My java knowledge isn't that high, still just a beginner

Edit: We have learned instance variables as well as touched a little upon return statements, we haven't learned anything about passby. By return I mean giving the driver the correct value, sorry if that threw you off

Edit2: Thank youuu so much user1710742, finally got it. I understand now. Makes sense

Upvotes: 0

Views: 11483

Answers (5)

Neuron
Neuron

Reputation: 5831

I am not totaly sure if you are actually doing what you want to do.. To me, it seems like you want to create a class called Bank and have a private variable there, called ballance. then methods for manipulating that value..

public static class Bank{
    private int balance = 0;

    // this method won't return any value. it will only change
    // the banks internal state, therefor we write "void"!
    public void deposit(int deposit){
        if(deposit<0){
            // using System.err instead of System.out will be handled differently
            // by java. It is meant to be used for error messages. some consoles make
            // these texts pop out more (like displyaing it in read)
            System.err.println ("Invalid Number");
        }else{
            // balance += deposit is a short way of writing balance = balance + deposit;
            balance += deposit;
        }
    }

    // this method will return an int value to who ever called it.
    // you can tell, because it says int after public
    public int getBalance(){
        return balance;
    }
}

And then a main method which handles the bank:

Bank bank = new Bank();
bank.deposit(5);
bank.deposit(8);

int balance = bank.getBalance(); // will be 13

System.out.println("balance is "+balance);

More on System.err can be found in the oracle docs about System

Hope I could help

Upvotes: 0

Pier-Alexandre Bouchard
Pier-Alexandre Bouchard

Reputation: 5235

  1. You should not name your method getDeposit() since the return type is void. Instead, just name it deposit(), because you actually do an action.

  2. Use a private instance variable named balance, in your Bank class.

  3. Use a getter for your balance field.

This snippet should work:

public class BankAccount {

    private int balance;

    public static void main(String[] args) {
        BankAccount account = new BankAccount();

        String deposit = JOptionPane.showInputDialog ("How much would you like to deposit?");
        account.deposit(Integer.parseInt (deposit));

        deposit = JOptionPane.showInputDialog ("How much would you like to deposit?");
        account.deposit(Integer.parseInt (deposit));

        System.out.println(account.getBalance());

    }

     public void deposit(int deposit){
         if (deposit < 0){
             System.out.println ("Invalid Number");
         }else{
             balance += deposit;
         }   
     }

     public int getBalance(){
         return balance;
     }
}

Upvotes: 0

stephen_combs
stephen_combs

Reputation: 1

start with the basics of the language. Yet, Keep in mind that understanding the problem is the only true way to reach the right solution. Don't let the language get in the way of the logic. You'll get the syntax down fast enough. You've got a good start. It's easy to get drawn into just finding A solution, instead of finding THE solution. The method of solving the problem is more important than having an answer. Try the link below for the basics on the return value.

http://www.homeandlearn.co.uk/java/java_methods.html

Upvotes: 0

ran88
ran88

Reputation: 105

You can add a new variable call dep with int data type :

int dep = myBank.getDeposit(deposit, balance);

after that change your method type from void to int and add return statement of variable that you want :

public int getDeposit(int deposit, int balance)
{
  if (deposit<0)
  {
    System.out.println ("Invalid Number");
    deposit=0;
  }
  balance =balance + deposit;
  return balance;
}

Upvotes: 0

Jackson
Jackson

Reputation: 579

The problem is you are attempting to change a copy of the variable, not the exact one that you gave to the method. When you exit the method, the copy is destroyed, and you are left with just the unchanged original value.

The method can't return a value unless you tell it to with a return statement.

public int getDeposit(int deposit, int balance)
{
    if (deposit<0)
    {
        System.out.println ("Invalid Number");
        deposit=0;
    }
    return balance + deposit;
}

call with: int total = myBank.getDeposit(deposit, balance);

Upvotes: 1

Related Questions