Well the reason it was showing the box was because "answer" was coming out as the value NaN which means "not a number". And that was because you were attempting to get access to "answer" when answer was defined to the local main function only.
So by moving answer to a higher scope other functions can see it and use it.
Then the second problem you had was that in your getPrincipal and getTerm functions you never set the principal or the term variables to what the user entered. So whatever the user entered wasn't being put into the calculation.
As soon as you make these changes, all was left was to modify your format pattern a little so that it would show numbers correctly and you were on easy street.
java
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class MortgageCalculator2
{
// Move answer to a higher scope so that many functions can see and manipulate it
private static double answer;
public static void main(String[] args)
{
//declare variables
double principal, interest, monthlyInterest;
int term, numbPayments;
//calling methods
principal = getPrincipal();
interest = getInterest()/100;
term = getTerm();
monthlyInterest = interest/12;
numbPayments = term*12;
answer = (principal*(monthlyInterest*Math.pow((1+monthlyInterest),numbPayments)))/(Math.pow((1+monthlyInterest),numbPayments) -1);
output();
finish();
}
//The getPrincipal() method will ask the user to input the principal amount
public static double getPrincipal()
{
//declare method variable
double principal = 0.0;
// Don't forget to set principal to what was entered
String principle = JOptionPane.showInputDialog(null, "Enter the Mortgage amount\n(do not use commas or dollar signs)\n or click Cancel to exit:");
principal = Double.parseDouble(principle);
return principal;
}
//The getInterest() method will retrieve the interest amount
public static double getInterest()
{
//declare method variable
double interest = 0.0;
boolean done = false;
//loop while not done
while (!done)
{
try
{
String rate = JOptionPane.showInputDialog(null, "Enter the interest rate\n(do not use the percentage sign)");
interest = Double.parseDouble(rate);
//test for interest rate amount
if (interest<0.0) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please enter an interest rate.", "Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return interest;
}
//the getTerm() method will get the term length of the loan
public static int getTerm()
{
//declare method variable
int term = 0;
// Again, make sure you set term to the value entered
String number = JOptionPane.showInputDialog(null, "Enter the length of mortgage term, use numbers only:");
term = Integer.parseInt(number);
return term;
}
//the output() method will display the monthly payment for the loan
public static void output()
{
// Change the pattern here to only show numbers and never leading zeros.
DecimalFormat twoDigits = new DecimalFormat("$#,##0.00");
JOptionPane.showMessageDialog(null,"Your Monthly Payment is:" + twoDigits.format(answer));
}
//the finish() method ends the program
public static void finish()
{
System.exit(0);
}
}
You will see the changes I made with in-code comments explaining what we did there. I simply moved answer to a higher scope so that your output function could see it and print it and made a few changes to the way you were setting values in your principal and term functions.
Enjoy!
"At DIC we be principal and term fixing code ninjas... our terms are no holds barred and our favorite person to beat up is the principal."
This post has been edited by Martyr2: 20 Dec, 2008 - 02:57 PM