2
public BigDecimal calculateTotal() {
    BigDecimal percent = BigDecimal.valueOf(0.9);
    int i = 0;
    BigDecimal price = BigDecimal.valueOf(0.0);
    while(!myOrders.isEmpty()){
        if (!myOrders.get(i).getItem().isBulk() && myMembership == true){
            price = price.add(myOrders.get(i).calculateOrderTotal().multiply(percent));
            myOrders.remove(i);
        }
        else{
            price = price.add(myOrders.get(i).calculateOrderTotal());
            myOrders.remove(i);
        }
    }
    //WHY IS THIS UNREACHABLE?!
    return price.setScale(2, RoundingMode.HALF_EVEN);
}

I know that anything after a return statement is unreachable code, but my only return statement is unreachable and I can't figure out why. The while loop is the way it is because I'm grasping at straws, I'm aware that it probably won't do what I want it to do. myOrders is an ArrayList.

4 Answers4

2

EDIT: Since OP said it is an ArrayList, my answer no longer applies.

You never update your index i. This should work:

public BigDecimal calculateTotal() {
    BigDecimal percent = BigDecimal.valueOf(0.9);
    int i = 0;
    BigDecimal price = BigDecimal.valueOf(0.0);
    while(!myOrders.isEmpty()) {
        if (!myOrders.get(i).getItem().isBulk() && myMembership == true) {
            price = price.add(myOrders.get(i).calculateOrderTotal().multiply(percent));
            myOrders.remove(i);
        } else {
            price = price.add(myOrders.get(i).calculateOrderTotal());
            myOrders.remove(i);
        }
        i++;    // <-- You were missing this
    }
    // Not unreachable anymore :)
    return price.setScale(2, RoundingMode.HALF_EVEN);
}
yiwei
  • 4,022
  • 9
  • 36
  • 54
  • OP said that the object was an ArrayList, and since ArrayLists will automatically rearrange themselves upon the removal of an element, the lack of incrementation of the index shouldn't matter. – Michael Fourre Oct 17 '15 at 06:20
2

Your variable i is never incremented. Depending on what type of Collection myOrders is, removing the 0th element each time may not shift the elements in the collection, and myOrders will never be empty.

Ling Zhong
  • 1,744
  • 14
  • 24
1

There is nothing in the posted code to explain the error. Since you said your IDE is Eclipse, I suggest to clean the project. Also, make sure to fix all other errors before looking at this one. This error doesn't make sense, I suspect you have other compiler errors in your project, which somehow cause this as a strange side effect. After you fix everything else, this one should naturally disappear.

Btw, to see clearer, here's a cleaned up version of the same code, doing exactly the same thing:

BigDecimal percent = BigDecimal.valueOf(0.9);
BigDecimal price = BigDecimal.ZERO;

while (!myOrders.isEmpty()) {
    Order first = myOrders.get(0);
    BigDecimal subtotal = first.calculateOrderTotal();
    if (!first.getItem().isBulk() && myMembership) {
        subtotal = subtotal.multiply(percent);
    }
    price = price.add(subtotal);
    myOrders.remove(0);
}
return price.setScale(2, RoundingMode.HALF_EVEN);
janos
  • 120,954
  • 29
  • 226
  • 236
-1

Cleaning eclipse solved the problem.