I want to make just a simple function that sums all with same name.
I have mapped JDO like this:
costName Cost
food 10
food 10
water 5
food 10
total of food 30
total of water 5
With this code I get total sum of all (this case 35)
int totalsum=0;
int sumCap=0;
for (JDO total : table) {
totalsum += total.getCost() ;
}
How do I get the cost for only food or/and water?
Should be simple but I don’t get it.
The above calculates all totals in one pass and then allows you to retrieve any you desire. You could also look for a specific name in the loop and only add the values that match.