How to accumulate a value in java?

7.2k views Asked by At

Right now, I'm learning all about class, constructors and instances. I've made a small bit of code in java and I'm stuck with this particular code.

***MY CODE OUTPUT IS SUPPOSE TO BE LIKE THIS:

OUTPUT

Enter amount:500

Do you want to enter amount again?(y/n):y

Enter amount:45

Do you want to enter amount again?(y/n):n

TOTAL:545

***BUT INSTEAD MY OUTPUT IS LIKE THIS:

OUTPUT

Enter amount:500

Do you want to enter amount again?(y/n):y

Enter amount:45

Do you want to enter amount again?(y/n):n

TOTAL:45

***It is not adding the amount that I enter throughout the loop and instead, it is giving me the very last amount that I input.

Here is the first code:

public class Test {
private double money;

public Test(){

}

public void addPoints(double money1){
    money += money1;
}

public int getMoney(){
    return money;
}

}

and the second code is here:

import java.util.Scanner;
public class testBody {
   public static void main(String[]args){
    double cashMoney;
    String response = "";
    Scanner hold = new Scanner(System.in);

    do{


    System.out.print("Enter amount:");
    cashMoney = hold.nextDouble();
    Test cashPlus = new Test();
    cashPlus.addPoints(cashMoney);


    System.out.print("Do you want to enter amount again?(y/n):");
    response = hold.next();

    if(response.equalsIgnoreCase("n")){

        System.out.print("TOTAL: " + cashPlus.getMoney());
    }

    }while(response.equalsIgnoreCase("y"));
}

}

5

There are 5 answers

0
Eran On BEST ANSWER

You should create the Test instance before the loop instead of in each iteration.

Test cashPlus = new Test();
do {
    System.out.print("Enter amount:");
    cashMoney = hold.nextDouble();
    cashPlus.addPoints(cashMoney);
    System.out.print("Do you want to enter amount again?(y/n):");
    response = hold.next();

    if(response.equalsIgnoreCase("n")){
        System.out.print("TOTAL: " + cashPlus.getMoney());
    }
} while(response.equalsIgnoreCase("y"));

Each time you create a new Test instance, cashMoney is initialized to 0 (since each instance has its own value of that member). When you finally print cashPlus.getMoney(), you print the value of the last instance you created, to which you only added the final amount you entered.

0
seb-o-matic On

In the line Test cashPlus = new Test(); you are creating a new Test object every time a value is entered. This effectively resets the already existing sum, because in the new class Test.money equals 0 again.

0
Curmudgeon On

The issue is this line being inside the do/while loop:

Test cashPlus = new Test();

The Test class object holds the variable money (which is initialized to 0) and you are creating a new instance of it with each iteration of the do/while loop, thus resetting cashPlus.money to 0.

Just move that line before of the do/while and you should be fine!

0
Uma Kanth On

replace

private double money;

with

private static int money;

Everytime you set the value, it becomes 0. To pertain the value make it a static variable.

Note that the return type of getMoney is int and the datatype of money is double. This has to be type-casted or changed to similar data-type.

0
Abhijit Bashetti On

In the loop , you are creating

Test cashPlus = new Test();

with this statement every time you are creating new object and adding sum to it. In all it is added to a new value as with new object it initialized to 0.

To avoid this instantiate the

Test cashPlus = new Test();

before starting the loop.