# Mortgage calculator with GUI

• (2 Pages)
• 1
• 2

## 29 Replies - 3388 Views - Last Post: 27 September 2010 - 10:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=192046&amp;s=2555332f08ad5509d440730ce5917c5b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 pcornacchione

Reputation: 0
• Posts: 29
• Joined: 02-September 10

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 10:24 AM

Excellent! Moved the button group to pane row4 and now they work. However my math is screwed. Help with that now? I get NaN when i try to calculate the monthly payment and nothing at all for the amortization.

```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 MortgageTest 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 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));

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 MortgageTest() {

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

row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height));
row1.setBorder( rowborder);

row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height));
row2.setBorder( rowborder);

row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
row3.setBorder( rowborder);

ButtonGroup pickOneGroup = new ButtonGroup();										//creates group for buttons
row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
row4.setBorder( rowborder);

monthlyPayment_txt.setEditable(false);										//user cannot alter this text
row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
row5.setBorder( rowborder);

row6.setMaximumSize( new Dimension( 10000, row6.getMinimumSize().height));
row6.setBorder( rowborder);

scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));        		//for amortization display

pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
setVisible(true);
setContentPane(pane);

}

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
balance = balance - premium;									//Determines loan balance

String titles = "Payment # \t MonthlyPayment \tLoan Balance \t Interest Paid";
displayArea.setText(titles);

while (number <= months)
{
interestPaid = balance * monthlyInterest + .005;				//Determines interest paid amount
balance = balance - premium;									//Determines loan balance
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);
number++;
}
}
}
}
public static void main(String[] args) {
Frame f = new MortgageTest();
f.show();

}
}
```

This post has been edited by pcornacchione: 27 September 2010 - 12:27 PM

### #17 pcornacchione

Reputation: 0
• Posts: 29
• Joined: 02-September 10

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 05:47 PM

I think i screwed up the looping on my math calculations for this mortgage calculator. I cannot figure out where i did it at. Can someone please take a look and give me a hand? Its supposed to display the monthly payment at the top of the GUI but i only get "NaN" for the results and then the amortization schedule at the bottom which does not display at all. All you java gurus have been super for helping me thus far but now i'm desperate because this program is due in a few hours and i still don't have it working correctly!! 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 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));

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

row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height));
row1.setBorder( rowborder);

row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height));
row2.setBorder( rowborder);

row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
row3.setBorder( rowborder);

ButtonGroup pickOneGroup = new ButtonGroup();									//creates group for buttons
row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
row4.setBorder( rowborder);

monthlyPayment_txt.setEditable(false);									//user cannot alter this text
row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
row5.setBorder( rowborder);

row6.setMaximumSize( new Dimension( 10000, row6.getMinimumSize().height));
row6.setBorder( rowborder);

scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));        	//for amortization display

pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
setVisible(true);
setContentPane(pane);

}

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
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();

}
}
```

### #18 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 05:55 PM

Duplicate topics merged. Please avoid duplicate posting.

### #19 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 06:37 PM

If you have a Not A Number it is you screw up with 0 values as this println() shows

```    			monthlyInterest = (interestRate/12)/100;					//Determines monthly interest rate
months = length * 12;										//Changes years to months
System.out.println("principal " + principal + " interest " + monthlyInterest + " months " + months);
monthlyPayment	= .005+(principal*monthlyInterest/(1-(Math.pow((1+monthlyInterest),(-months)))));		//Determines monthlyPayment

```

principal 12000.0 interest 0.0 months 0

### #20 pcornacchione

Reputation: 0
• Posts: 29
• Joined: 02-September 10

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 06:44 PM

pbl--I'm confused. Do you mean i have the print line in the wrong location? I looked at my coding and i'm sorry but i don't understand what you mean.

### #21 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 07:53 PM

pcornacchione, on 27 September 2010 - 07:44 PM, said:

pbl--I'm confused. Do you mean i have the print line in the wrong location? I looked at my coding and i'm sorry but i don't understand what you mean.

I just added a System.out.prinltn() statement to show you that when you calculate monthlyPayment

monthlyInterest and months are 0

### #22 pcornacchione

Reputation: 0
• Posts: 29
• Joined: 02-September 10

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 08:02 PM

pbl--i really do thank you for replying. But i'm sorry i simply don't understand. I don't know how to fix this because i don't see where you get the fields are empty. I guess i'm just beyond stupid. Two more weeks of Java hell...i don't think i will survive.

### #23 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 08:12 PM

Here is your error... I think

```   if(command == calculateButton) {
double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users

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;
}

```

event.getSource() will never be one of your radio button it is actally calculateButton
You have to do
```   if(command == calculateButton) {
double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users

if (oneButton.isSelected()) {
length = 7;
interestRate = 5.35;
}
else if (twoButton.isSelected()) {
length = 15;
interestRate = 5.5;
}
else {
length = 30;
interestRate = 5.75;
}
// now you can calculate

```

### #24 pcornacchione

Reputation: 0
• Posts: 29
• Joined: 02-September 10

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 08:22 PM

pbl--bless you!! I figured it was gonna be something fairly simple but i'm just so stressed right now i couldn't see it. Now on to fixing the amortization portion. Will this nightmare ever end? **sigh**

### #25 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 08:48 PM

pcornacchione, on 27 September 2010 - 09:22 PM, said:

pbl--bless you!! I figured it was gonna be something fairly simple but i'm just so stressed right now i couldn't see it. Now on to fixing the amortization portion. Will this nightmare ever end? **sigh**

First you have to do the same thing about which radio button is selecteded
then you have to append() to the textArea. Doing setText() erase what was there before

```		if(command == amortizationButton) {
double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input
if (oneButton.isSelected()) {
length = 7;
interestRate = 5.35;
}

else if (twoButton.isSelected()) {
length = 15;
interestRate = 5.5;
}

else {
length = 30;
interestRate = 5.75;
}

String titles = "Payment # \t MonthlyPayment \tLoan Balance \t Interest Paid";
displayArea.setText(titles);
for(int number = 0; number <= length - 1; number++) {

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
balance = balance - premium;								//Determines loan balance

displayArea.append("\n" + 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);
}

```

### #26 pcornacchione

Reputation: 0
• Posts: 29
• Joined: 02-September 10

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:04 PM

pbl--thank you so very much for working with me!! I still have a problem with the looping. It isn't printing the full amortization...only one line. But it looks great!! You are my Java Guru!! Here's the updated program...

```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 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));

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

row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height));
row1.setBorder( rowborder);

row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height));
row2.setBorder( rowborder);

row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
row3.setBorder( rowborder);

ButtonGroup pickOneGroup = new ButtonGroup();									//creates group for buttons
row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
row4.setBorder( rowborder);

monthlyPayment_txt.setEditable(false);									//user cannot alter this text
row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
row5.setBorder( rowborder);

row6.setMaximumSize( new Dimension( 10000, row6.getMinimumSize().height));
row6.setBorder( rowborder);

scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));        	//for amortization display

pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
setVisible(true);
setContentPane(pane);

}

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;
}

else if (event.getSource() == twoButton) {
length = 15;
interestRate = 5.5;
}

else  {         //(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;
}

else if (event.getSource() == twoButton) {
length = 15;
interestRate = 5.5;
}

else  {          //(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
balance = balance - premium;								//Determines loan balance

String titles = "\t Payment # \t\t Loan Balance \t\t Interest Paid \n";
displayArea.setText(titles);
displayArea.append("\t"+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();

}
}
```

### #27 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:26 PM

You have to set length BEFORE entering the loop as I did
without that length will be == 0 and the loop never performed

Also use the same technique
if(oneButton.isSelected())
not
if(event.getSource() == oneButton)

at least cut & paste correctly

```if(command == amortizationButton) {
double principal=Double.parseDouble(principal_txt.getText());	//retrieving the users input
if (oneButton.isSelected()) {
length = 7;
interestRate = 5.35;
}

else if (twoButton.isSelected()) {
length = 15;
interestRate = 5.5;
}

else {
length = 30;
interestRate = 5.75;
}

String titles = "Payment # \t MonthlyPayment \tLoan Balance \t Interest Paid";
displayArea.setText(titles);
for(int number = 0; number <= length - 1; number++) {

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
balance = balance - premium;								//Determines loan balance

displayArea.append("\n" + 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);
}

```

### #28 pcornacchione

Reputation: 0
• Posts: 29
• Joined: 02-September 10

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:49 PM

pbl--you are my savior!! Still have math errors but it works! The amortization only runs to month 29 and the balance and interestPaid are off. I used the exact same math calculations that worked fine for the last assignment so i don't get it. But i'm so happy now!! THANK YOU THANK YOU THANK YOU!! You have made a stupid newbie very happy tonight!!

UPDATE--got the math almost worked out but i still don't see the error for the loop to get it past 29??

UPDATE--math corrected!! Just need to fix the looping error so more than 29 payments will show!! Suggestions??

This post has been edited by pcornacchione: 27 September 2010 - 10:04 PM

### #29 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 10:25 PM

Length is years, not months:

```...
for(int number = 0; number <= length - 1; number++) {
...

```

### #30 pcornacchione

Reputation: 0
• Posts: 29
• Joined: 02-September 10

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 10:34 PM

n8wxs, on 27 September 2010 - 09:25 PM, said:

Length is years, not months:

```...
for(int number = 0; number <= length - 1; number++) {
...

```

Geez, all my problems have been so simple and i've been so blind! LOL Thank you so much for pointing that out!! Everyone here is so great!!