Everyting in this program works except one part. I have to
put a NullPointException in this program to catch if someone pushes
the "Cancel Button" on the second dialog box. This may seem like an
easy task for most, this is my first time. This is all I have left
to do, and I can't figure it out. I know someone out there could
tell me how to do this, and I would appreciate it.
Dave
Here is my Program, what Am I doing wrong?
import javax.swing.JOptionPane;
import java.text.*;//import javax.swing.*;
import java.lang.*;
public class NewEnhancedInvoiceApp{
public static void main(String[] args){ // start of main method
double discountAmount = 0;
//double invoiceTotal;
String choice = "";
while (!(choice.equalsIgnoreCase("n"))) {
String inputString = JOptionPane.showInputDialog(
"Enter order total: ");
double orderTotal = parseTotal (inputString);
// code that calculates and displays new total
if (orderTotal >= 500 )
discountAmount = orderTotal * .2;
else if (orderTotal >= 250 && orderTotal < 500)
discountAmount = orderTotal * .15;
else if (orderTotal >= 100 && orderTotal <
250)
discountAmount = orderTotal * .1;
else if (orderTotal < 100)
discountAmount = 0.0;
double invoiceTotal = orderTotal - discountAmount;
//Format Order Total, Discount Amount and Invoice Total
//to display numeric expression as currency,
NumberFormat currency = NumberFormat.getCurrencyInstance();
String message = "Order Total: " + currency.format(orderTotal) +"\n"
+"Discount Amount: " +
currency.format(discountAmount)+ "\n"
+"Invoice Total: " + currency.format
(invoiceTotal) + "\n\n"
+ "To Continure, Press O.K. \n"
+ "Continure Y / N ?";
choice = JOptionPane.showInputDialog(null, message, "Invoice
Application", JOptionPane.PLAIN_MESSAGE);
if (choice == "y") break;
} // end while loop
System.exit(0);
}
private static double parseTotal(String totalString){
double orderTotal = 0;
boolean tryAgain = true;
while (tryAgain) {
try{
orderTotal = Double.parseDouble(totalString);
while (orderTotal <= 0) {
totalString =
JOptionPane.showInputDialog(
"Invalid order total. \n"
+"Please enter a positive
number: ");
orderTotal =
Double.parseDouble(totalString);
}
tryAgain = false;
}
catch(NumberFormatException e) {
totalString = JOptionPane.showInputDialog(
"Invalid order total. \n"
+"Please enter a number: ");
//try{
//}
/*catch(NullPointerException rex) {
System.out.println ("Please Enter Data");
totalString = JOptionPane.showInputDialog(null,
"Invalid order total. \n"
+ "Please enter a number: ",
"Invoice", JOptionPane.ERROR_MESSAGE);
*/
}
}
return orderTotal;
}
}
//}