You should use a numeric data type for arithmetic operations (not Object
). With out seeing the code in add()
my recommendation is to store the total price in a double
primitive.
double price =0;
for(int x = 0; x < rprice.size(); x++) {
//you may need to cast/convert here
price += (double)rprice.get(x);
//what does this do????
Object numResult = add(totalPrice, price);
pricelbl.setText(price);
}
You should definitely not be using Object
according to your code. Simply use basic JAVA type such as int, float…
In this way : float totalPrice = 0;
Then if your get
method send back an object, just cast it, but you should change it aswell, so it returns a float…
You will also need to change your add(Object,Object)
method, don’t use Object if you know what’s the type of the variable you are working with.
try something like this:
public void calculateprice(ArrayList rprice){
Object totalPrice = 0;
for(int x = 0; x < rprice.size(); x++) {
Object price = rprice.get(x);
totalPrice = add(totalPrice, price);
}
pricelbl.setText(totalPrice);
}
But this way very bad(your code smell). Use Numeric types for calculations.
Much better will be:
public void calculateprice(ArrayList<Integer> rprice){
Integer totalPrice = 0;
for(int x = 0; x < rprice.size(); x++) {
Integer price = rprice.get(x);
totalPrice += price;
}
pricelbl.setText(totalPrice);
}
Actually, this code also not a perfect. Best variant will be:
public Integer calculateprice(List<Integer> prices){
Integer totalPrice = 0;
for(int x = 0; x < prices.size(); x++) {
Integer price = prices.get(x);
totalPrice += price;
}
return totalPrice;
}