This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I don't understand why this happens in this code. The only difference I see is that the first two numbers are doubles and the third is an int, but the exception is caught for the first two and not the third. The third throws the NumberFormatException. But if I hit Cancel BEFORE parseInt() is called, why am I getting a NumberFormatException? I couldn't find anything in the docs that might explain this. Thanks in advance. import javax.swing.*; import java.text.*; public class FutureValue { public static void main(String[] args) { String choice = ""; try { while(!(choice.equalsIgnoreCase("x"))) { //Works as expected here. String paymentString = JOptionPane.showInputDialog(null, "Enter monthly payment: ", "Future Value", JOptionPane.PLAIN_MESSAGE); double monthlyPayment = Double.parseDouble(paymentString);
System.exit(0); }//end main private static double calculateFutureValue(double monthlyPayment, int months, double interestRate) { int i = 1; double futureValue = 0; while (i <= months) { futureValue = (futureValue + monthlyPayment) * (1 + interestRate); i++; }//end while
return futureValue;
}//end calculateFutureValue }//end class
John Jaspers
Greenhorn
Joined: Mar 27, 2003
Posts: 9
posted
0
I'm sorry. I should have made my question clearer. The following code is supposed to catch a NullPointerException in any of the JOptionPanes if one presses cancel. This works just fine in the first two, but the third seems to always try to assign to the int. The NullPointerException should occur before the assignment, but it seems to always occur at the assignment and throw a NumberFormatException. Could anyone explain to me why this happens here and not in the two previous boxes? Thanks again. [ March 28, 2003: Message edited by: John Jaspers ]
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
It has nothing to do with JOptionPane as you can see from this:
Any Java source code geeks around? [ March 28, 2003: Message edited by: Barry Gaunt ]