How do I display a price up to two decimals (cents) in Java? I have tried different methods but can't seem to get it right.
import java.io.; import java.util.;
public class Menu {
public static void main(String[] args){
String apptName = "";
String entreeName = "";
String dessertName = "";
double apptPrice = 0.00;
double entreePrice = 0.00;
double dessertPrice = 0.00;
try {
Scanner sc = new Scanner(new File("menu.txt"));
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] item = line.split(", ");
int type = Integer.parseInt(item[2]);
String name = item[0];
double price = Double.parseDouble(item[1].substring(1));
String s = String.format("$%.2f", 2.50);
if (type == 1) {
if (price > apptPrice) {
apptName = name;
apptPrice = price;
}
} else if (type == 2) {
if (price > entreePrice) {
entreeName = name;
entreePrice = price;
}
} else {
if (price > dessertPrice) {
dessertName = name;
dessertPrice = price;
}
}
}
double total = apptPrice + entreePrice + dessertPrice;
System.out.println("Please enter the filename you wish to process: Most Expensive Meal");
System.out.println("-------------------");
System.out.println("Appetizer : "+apptName);
System.out.println("Entree : "+entreeName);
System.out.println("Dessert : "+dessertName);
System.out.printf("Total Price: $"+total);
} catch (FileNotFoundException fe) {
System.out.println("Sorry, input file: was not found.");
}
}
}