Here is my code:
public void checkOut() {
double sum;
System.out.println("Checking out items ...");
for (int i = 0; i < purchases.length; i++) {
sum =+ purchases[i].getPrice();
System.out.println(purchases[i].getDescription() + "/" + purchases[i].getPrice());
}
System.out.println("Amount due: " + "$" + new DecimalFormat("0.00").format(sum));
}
When I compile it I get this error:
The local variable sum may not have been initialized.
Alternatively when I change the sum line to double sum = sum + purchases[i].getPrice();
I get the following error:
sum cannot be resolved to a variable.
Its basically a method that takes the list of items placed in a shopping basket; prints the items and their individual price, then finds the total price (sum) of the items.
Can anyone please tell me what I'm doing wrong?