i'm trying to calculate the total amount from the text file, so far I have printed the prices out but i'm not sure what to code to use to calculate the total amount.
OrderPage.setText("Diner Number | Food | Quantity | Calories | Price");
DefaultListModel listModel = new DefaultListModel();
try {
FileReader file = new FileReader("savedFoodData.txt");
BufferedReader buffer = new BufferedReader(file);
while ((line = buffer.readLine()) != null) {
outputDinerChoice = line;
outputDinerChoice = outputDinerChoice.replaceAll(",", " ");
listModel.addElement(outputDinerChoice);
dinersChoice = outputDinerChoice.split(" ");
System.out.println(dinersChoice[4]);
}
the file ("savedFoodData.txt") would set out like this:
"Diner Number | Food | Quantity | Calories | Price"
Which would contain:
1/2,Burger,1,156kcal,£2.70
1/2,Chicken,1,159kcal,£3.90
1/2,Steak,1,50kcal,£7.00
2/2,Noodles,1,398kcal,£4.90
2/2,Pizza,1,156kcal,£2.70
1/2,Beer,1,20kcal,£4.10
1/2,Coke Tea,1,5kcal,£1.50
And this code would print out
System.out.println(dinersChoice[4]);
£2.70
£3.90
£7.00
£4.90
£2.70
£4.10
£1.50
And I'm trying to calculate the total price from this, how would I do this?
Example
String line = "1/2,Burger,1,156kcal,£2.70";
String[] columns = line.split(",");
double value = Double.parseDouble(columns[4].substring(1));
Integrated
double sum = 0;
while ((line = buffer.readLine()) != null) {
String[] columns = lines.split(",");
sum = sum + Double.parseDouble(columns[4].substring(1));
}