The monthly payment only returns "NaN" and the amortization does not display at all. I'm pretty sure my problem lies in my looping but i'm lost and unable to find it. HELP!!!!!
import java.awt.*; import java.awt.event.*; import java.math.*; import java.text.*; import java.lang.*; import java.util.Locale; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.JOptionPane; public class Mortgage3 extends JFrame implements ActionListener { double principal = 0; //principal int length = 0; //years int months = 0; //months double interestRate = 0; //interest rate double monthlyInterest = 0; //monthly interest rate double monthlyPayment = 0; //monthly payment double premium = 0; //premium double interestPaid = 0; //interest paid double balance = principal; //balance DecimalFormat money = new DecimalFormat("$0.00"); //formats to 2 decimal places JPanel row1 = new JPanel(); JLabel mortgage_label = new JLabel("Pam's Mortgage Calculator", JLabel.CENTER); JPanel row2 = new JPanel(new GridLayout(1, 2)); JLabel principal_label = new JLabel("Enter Mortgage Amount (do not enter $ or cents):",JLabel.LEFT); JTextField principal_txt = new JTextField(10); JPanel row3 = new JPanel(new GridLayout(1, 2)); JLabel button_label = new JLabel("Please select one of the following options:", JLabel.CENTER); JPanel row4 = new JPanel(new GridLayout(1, 3)); JRadioButton oneButton = new JRadioButton("7 years at 5.35%",true); //creates buttons JRadioButton twoButton = new JRadioButton("15 years at 5.5%"); JRadioButton threeButton = new JRadioButton("30 years at 5.75%"); JPanel row5 = new JPanel(new GridLayout(1, 2)); JLabel monthlyPayment_label = new JLabel("Your monthly payment will be:", JLabel.LEFT); JTextField monthlyPayment_txt = new JTextField(10); JPanel row6 = new JPanel(new GridLayout(1, 2)); JButton resetButton = new JButton("Reset"); JButton exitButton = new JButton("Exit"); JButton calculateButton = new JButton("Calculate Monthly Payment"); JButton amortizationButton = new JButton("Show Amortization"); JTextArea displayArea = new JTextArea(10, 45); //for amortization display JTextField number_txt = new JTextField(4); JTextField balance_txt = new JTextField(12); JTextField interestPaid_txt = new JTextField(12); JScrollPane scroll = new JScrollPane(displayArea); public Mortgage3() { super ("Pam's Mortgage Calculator"); setSize(750, 600); //sets overall GUI size setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container pane = getContentPane(); Border rowborder = new EmptyBorder( 10, 15, 5, 15 ); //sets spacing inside frame pane.add(row1); row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height)); row1.setBorder( rowborder); pane.add(row2); row2.add(principal_label); row2.add(principal_txt); row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height)); row2.setBorder( rowborder); pane.add(row3); row3.add(button_label); row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height)); row3.setBorder( rowborder); pane.add(row4); ButtonGroup pickOneGroup = new ButtonGroup(); //creates group for buttons pickOneGroup.add(oneButton); pickOneGroup.add(twoButton); pickOneGroup.add(threeButton); row4.add(oneButton); row4.add(twoButton); row4.add(threeButton); row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height)); row4.setBorder( rowborder); pane.add(row5); row5.add(monthlyPayment_label); row5.add(monthlyPayment_txt); monthlyPayment_txt.setEditable(false); //user cannot alter this text row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height)); row5.setBorder( rowborder); pane.add(row6); row6.add(resetButton); row6.add(exitButton); row6.add(calculateButton); row6.add(amortizationButton); row6.setMaximumSize( new Dimension( 10000, row6.getMinimumSize().height)); row6.setBorder( rowborder); scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //for amortization display pane.add(scroll); pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS)); setVisible(true); setContentPane(pane); oneButton.addActionListener(this); //button one listener twoButton.addActionListener(this); //button two listener threeButton.addActionListener(this); //button three listener resetButton.addActionListener(this); //reset listener exitButton.addActionListener(this); //exit listener calculateButton.addActionListener(this); //calculate listener amortizationButton.addActionListener(this); //amortization listener } public void actionPerformed(ActionEvent event) { Object command = event.getSource(); //begins error message checking if(command == calculateButton) { try { principal = Double.parseDouble(principal_txt.getText()); } catch(NumberFormatException e) { //error message for principal amount JOptionPane.showMessageDialog(null, "Dollar amounts only please! (ex. 300000)", "ERROR", JOptionPane.ERROR_MESSAGE); } } //resets all amounts to blank if(command == resetButton) { principal_txt.setText(null); monthlyPayment_txt.setText(null); displayArea.setText(null); } //exits the program if(command == exitButton) { System.exit(0); } if(command == calculateButton) { double principal=Double.parseDouble(principal_txt.getText()); //retrieving the users input if (event.getSource() == oneButton) { length = 7; interestRate = 5.35; } if (event.getSource() == twoButton) { length = 15; interestRate = 5.5; } if (event.getSource() == threeButton) { length = 30; interestRate = 5.75; } monthlyInterest = (interestRate/12)/100; //Determines monthly interest rate months = length * 12; //Changes years to months monthlyPayment = .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months))))); //Determines monthlyPayment String s = Double.toString(monthlyPayment); monthlyPayment_txt.setText(s); } if(command == amortizationButton) { double principal=Double.parseDouble(principal_txt.getText()); //retrieving the users input for(int number = 0; number <= length - 1; number++) { if (event.getSource() == oneButton) { length = 7; interestRate = 5.35; } if (event.getSource() == twoButton) { length = 15; interestRate = 5.5; } if (event.getSource() == threeButton) { length = 30; interestRate = 5.75; } monthlyInterest = (interestRate/12)/100; months = length * 12; //Changes years to months monthlyPayment = .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months))))); //Determines monthlyPayment interestPaid = balance * monthlyInterest + .005; //Determines interest paid amount premium = monthlyPayment - interestPaid; //Determines premium paid balance = balance - premium; //Determines loan balance String titles = "Payment # \t MonthlyPayment \tLoan Balance \t Interest Paid"; displayArea.setText(titles); displayArea.setText(number+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid)); String s = Double.toString(number); number_txt.setText(s); balance_txt.setText(s); interestPaid_txt.setText(s); } } } public static void main(String[] args) { Frame f = new Mortgage3(); f.show(); } }