# Java Mortgage Calculator GUI Help

Page 1 of 1

## 2 Replies - 10602 Views - Last Post: 07 September 2010 - 05:58 AMRate 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=189290&amp;s=5ab580cca2d6b588ba8eb1b286397a8f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MrsE

Reputation: 0
• Posts: 35
• Joined: 06-September 10

# Java Mortgage Calculator GUI Help

Posted 06 September 2010 - 05:41 PM

Write the program in Java (with a graphical user interface) and have it calculate and display the
mortgage payment amount from user input of the amount of the mortgage and the user's
selection from a menu of available mortgage loans:
- 7 years at 5.35%
- 15 years at 5.5%
- 30 years at 5.75%
Use an array for the mortgage data for the different loans. Display the mortgage payment amountfollowed by the loan balance and interest paid for each payment over the term of the loan. Allow
the user to loop back and enter a new amount and make a new selection or quit. Please insert
comments in the program to document the program.

Here are my errors:

double cannot be dereferenced Line 112
double cannot be dereferenced Line 113

Here is my code:
```/** Mortgage Calculator Program
* Week 2 PRG421
*Change Request #4
*Requestor: Ninfa Pendleton - Rapid City, SD
*Write the program in Java (with a graphical user interface) and have it calculate and display the
*mortgage payment amount from user input of the amount of the mortgage, the term of the
*mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data
*or quit. Please insert comments in the program to document the program.
*
*Author: Stacey Estey
*Due August 30, 2010*/

//import needed packages
import java.awt.*;
import javax.swing.*;
import javax.swing.JComboBox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

//declare calculator class
public class Calculator extends JFrame implements ActionListener
{

private JFrame frame;
private JLabel labelAmount;
private JLabel labelTerm;
private JLabel labelRate;
private JTextField textFieldAmount;
private JTextField textFieldMonthlyPayment;
private JComboBox termList;
private JComboBox rateList;
private JButton buttonCalc;

public Calculator()
{

initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setTitle("Mortgage Calculator");
pack();

}

public void initComponents()
{

//strings for combo boxes
String [] termString = {"7", "15", "30"};
String [] rateString = {"0.535", "0.055", "0.0575"};

//Initialize Components
frame = new JFrame();
labelAmount = new JLabel("Amount");
textFieldAmount = new JTextField();
labelTerm = new JLabel("Term");
termList = new JComboBox(termString);
labelRate = new JLabel("Rate");
rateList = new JComboBox(rateString);
textFieldMonthlyPayment = new JTextField();
buttonCalc = new JButton("Calculate");

//Set Object Attributes
textFieldMonthlyPayment.setEditable(false);
textFieldMonthlyPayment.setColumns(12);
textFieldAmount.setColumns(8);
termList.setSelectedIndex(2);
rateList.setSelectedIndex(2);

Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

// add the components to the panel

}

public static void main(String[] args)
{
Calculator frame = new Calculator();
}

private void setMonthlyPaymentValue()
{
DecimalFormat formatter = new DecimalFormat("\$#,##0.00");

double amount = Double.parseDouble(textFieldAmount.getText()); //get user input--mortgage amount
double termList = Double.toString(termString(termList.getSelectedItem()))* 12 ; //get user input--term of mortgage in years
double rateList = Double.toString(rateString(rateList.getSelectedItem()))/ 12 ; // get user input--interest rate
double monthlyPayment = amount * Math.pow(1+rateList, termList) * rateList /(Math.pow(1+rateList , termList)-1); //calculate input display monthly payment amount.

textFieldMonthlyPayment.setText(" " + formatter.format(monthlyPayment));

}
public void actionPerformed(ActionEvent event)
{

String command = event.getActionCommand();
if (command == "Calculate") {
setMonthlyPaymentValue();
}

}
}

```

This is as far as I have got. I am stuck trying to getting these lines of code right so I can move on to trying to get the rest done. Any advice would be greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: Java Mortgage Calculator GUI Help

### #2 macosxnerd101

• Games, Graphs, and Auctions

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

## Re: Java Mortgage Calculator GUI Help

Posted 06 September 2010 - 06:10 PM

The Double.toString() method converts the Double to a String. Then you are multiplying a String by an int, and assigning it to a Double. No need to use Double.toString() at all. Do you mean to use Double.parseDouble() instead?
```double termList = Double.toString(termString(termList.getSelectedItem()))* 12 ;

```

### #3 MrsE

Reputation: 0
• Posts: 35
• Joined: 06-September 10

## Re: Java Mortgage Calculator GUI Help

Posted 07 September 2010 - 05:58 AM

macosxnerd101, on 06 September 2010 - 05:10 PM, said:

The Double.toString() method converts the Double to a String. Then you are multiplying a String by an int, and assigning it to a Double. No need to use Double.toString() at all. Do you mean to use Double.parseDouble() instead?
```double termList = Double.toString(termString(termList.getSelectedItem()))* 12 ;

```

That is how i had it to start with, I have changed it back, but I still get the same error when I compile.
```/** Mortgage Calculator Program
* Week 2 PRG421
*Change Request #4
*Requestor: Ninfa Pendleton - Rapid City, SD
*Write the program in Java (with a graphical user interface) and have it calculate and display the
*mortgage payment amount from user input of the amount of the mortgage, the term of the
*mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data
*or quit. Please insert comments in the program to document the program.
*
*Author: Stacey Estey
*Due August 30, 2010*/

//import needed packages
import java.awt.*;
import javax.swing.*;
import javax.swing.JComboBox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

//declare calculator class
public class Calculator extends JFrame implements ActionListener
{

private JFrame frame;
private JLabel labelAmount;
private JLabel labelTerm;
private JLabel labelRate;
private JTextField textFieldAmount;
private JTextField textFieldMonthlyPayment;
private JComboBox termList;
private JComboBox rateList;
private JButton buttonCalc;

public Calculator()
{

initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setTitle("Mortgage Calculator");
pack();

}

public void initComponents()
{

//strings for combo boxes
String [] termString = {"7", "15", "30"};
String [] rateString = {"0.535", "0.055", "0.0575"};

//Initialize Components
frame = new JFrame();
labelAmount = new JLabel("Amount");
textFieldAmount = new JTextField();
labelTerm = new JLabel("Term");
termList = new JComboBox(termString);
labelRate = new JLabel("Rate");
rateList = new JComboBox(rateString);
textFieldMonthlyPayment = new JTextField();
buttonCalc = new JButton("Calculate");

//Set Object Attributes
textFieldMonthlyPayment.setEditable(false);
textFieldMonthlyPayment.setColumns(12);
textFieldAmount.setColumns(8);
termList.setSelectedIndex(2);
rateList.setSelectedIndex(2);

Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

// add the components to the panel

}

public static void main(String[] args)
{
Calculator frame = new Calculator();
}

private void setMonthlyPaymentValue()
{
DecimalFormat formatter = new DecimalFormat("\$#,##0.00");

double amount = Double.parseDouble(textFieldAmount.getText()); //get user input--mortgage amount
double termList = Double.parseDouble(termString(termList.getSelectedItem()))* 12 ; //get user input--term of mortgage in years
double rateList = Double.parseDouble(rateString(rateList.getSelectedItem()))/ 12 ; // get user input--interest rate
double monthlyPayment = amount * Math.pow(1+rateList, termList) * rateList /(Math.pow(1+rateList , termList)-1); //calculate input display monthly payment amount.

textFieldMonthlyPayment.setText(" " + formatter.format(monthlyPayment));

}
public void actionPerformed(ActionEvent event)
{

String command = event.getActionCommand();
if (command == "Calculate") {
setMonthlyPaymentValue();
}

}
}

```