Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Bad operand type for binary operator '+' [closed]

Tags:

java

arraylist

I'm working on an assignment where I need to do some work with ArrayLists. I declare one ArrayList early on in my program as

static ArrayList<Double> prices = new ArrayList<>();

and I want to use this arraylist in an enhanced for loop in a method later on. Currently the code I have for this method is

public static double getTotal(ArrayList<Double> prices) {
    double total = 0.0;
    for (double value: prices){
        total += (prices);
    }

    subTotal = total + (total * .0825); //add tax to get subTotal
    return subTotal;
}

I am trying to get the value total to = the total from the previous iteration + each value in the ArrayList prices. I'm pretty sure that the way I have it programmed is not the correct way to do it, but I am totally stumped on how to get this to work. Any ideas? If you need further clarification on what I'm trying to do or if you need more excerpts from my code I'm happy to provide it. Thanks!

like image 484
njwoodard Avatar asked Jul 14 '26 01:07

njwoodard


1 Answers

total += value

You should add the value, not the List.


Structure of a for each :

  1. For
  2. Type of the objects/primitives inside the list
  3. Name of the temporary copy of the variable from the list
  4. :
  5. Name of the list
like image 142
Yassin Hajaj Avatar answered Jul 15 '26 15:07

Yassin Hajaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!