[Solved] How can i add all of the prod obtained? [closed]


I would strongly consider breaking up your logic into smaller, readable methods. You also don’t need to store the numbers as fields.

I.e. Remove this whole block of code…

double num1, num2, prod1;
double num3, num4, prod2;
double num5, num6, prod3;
double num7, num8, prod4;
double num9, num10, prod5;

double grandt = prod1 + prod2 + prod3 + prod4 + prod5;

Also, Double.parseDouble will throw a NumberFormatException if the string that you are parsing is not a double value, so if your app is crashing for that reason, make sure those values are acually numbers.


Here is a small sample of what it looks like you are trying to achieve

private double getTotalMilk() {
    double quantity = Double.parseDouble(qtymilk.getText().toString());
    double amount = Double.parseDouble(milk.getText().toString());
    return quantity * amount;
}

private double getTotalTide() {
    double quantity = Double.parseDouble(qtytide.getText().toString());
    double amount = Double.parseDouble(tide.getText().toString());
    return quantity * amount;
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.button1) {
        double grandTotal = 0;

        double totalMilk = getTotalMilk();
        grandTotal += totalMilk;
        totalmilk.setText(Double.toString(totalMilk));

        double totalTide = getTotalTide();
        grandTotal += totalTide;
        totaltide.setText(Double.toString(totalTide));

        // Repeat pattern....

        grandtotal.setText(Double.toString(grandTotal));
    }
}

10

solved How can i add all of the prod obtained? [closed]