# Java Mortgage Calculator Problem

Page 1 of 1

## 2 Replies - 1124 Views - Last Post: 02 August 2010 - 08:09 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=184283&amp;s=f79c71ef6e5b0b585c7a4aa2c9feaf70&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kimber_blueeyes

Reputation: 0
• Posts: 1
• Joined: 02-August 10

# Java Mortgage Calculator Problem

Posted 02 August 2010 - 05:36 PM

I am struggling to get my Java program to work and any help would be very useful. When I try to compile the code I get the following error.

C:\Documents and Settings\Kim Jackson\My Documents\Phoenix\PRG421\CalculatorWk4.java:210: cannot find symbol
symbol : method principal(double)
location: class CalculatorWk4
double monthlyPayment = principal (perMonthRate/(1 - Math.pow((1 - perMonthRate),(-termMonths))));

```import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class CalculatorWk4 extends JFrame implements ActionListener {

int term = 0;
double principal = 0;
double rate = 0;
double monthlyPayment = 0;
double interest = 0;

String mTerm[] = {"7", "15", "30"};
String mInterst[] = {"5.35%", "5.50%", "5.75%"};

JPanel row1 = new JPanel();
JLabel mortgage_label = new JLabel("MORTGAGE PAYMENT CALCULATOR", JLabel.CENTER);

JPanel row2 = new JPanel(new GridLayout(1, 2));
JLabel principal_label = new JLabel("Mortgage Principal \$",JLabel.LEFT);
JTextField principal_txt = new JTextField(10);

JPanel row3 = new JPanel(new GridLayout(1, 2));
JLabel term_label = new JLabel("Mortgage Term (Yrs)",JLabel.LEFT);
JTextField term_txt = new JTextField(10);

JPanel row4 = new JPanel(new GridLayout(1, 2));
JLabel rate_label = new JLabel("Interest Rate (%)", JLabel.LEFT);
JTextField rate_txt = new JTextField(10);

JPanel row5 = new JPanel(new GridLayout(1, 2));
JLabel payment_label = new JLabel("Monthly Payment \$", JLabel.LEFT);
JTextField payment_txt = new JTextField(10);

//**************create buttons**************
JPanel button = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
JButton clearButton = new JButton("Clear");
JButton exitButton = new JButton("Exit");
JButton calculateButton = new JButton("Calculate");

//**************set textarea to diplay payments**************
JTextArea displayArea = new JTextArea(10, 45);
JScrollPane scroll = new JScrollPane(displayArea);

public CalculatorWk4()
{
super ("Kim Jackson's Mortgage Calc");
setSize(550, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();

Border rowborder = new EmptyBorder( 3, 10, 3, 10 );

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

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

ButtonGroup bgroup = new ButtonGroup();

payment_txt.setEnabled(false); //set payment amount uneditable
row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height));
row5.setBorder( rowborder);

button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height));

scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

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

}
public void actionPerformed(ActionEvent event){

Object command = event.getSource();

if(command == calculateButton)

{
try
{
Payment();
}

catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invaild Entry! Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
//*********Set rate and term based on which item in the combobox is selected*********

if(button_A.isSelected() == true)
{
Payment();
rate = 5.35;
term = 7;
}

if(button_B.isSelected() == true)
{
Payment();
rate = 5.5;
term = 15;
}

if (button_C.isSelected() == true)
{
Payment();
rate = 5.75;
term = 30;
}

if(command == clearButton)
{
principal_txt.setText(null);
term_txt.setText(null);
rate_txt.setText(null);
payment_txt.setText(null);
displayArea.setText(null);
}

if(command == exitButton)
{
System.exit(0);
}
}

//**************payment calculation****************

public void Payment()
{

double principal = Double.parseDouble(principal_txt.getText()); //Principal amount
float rate1 = Float.parseFloat((String)rate_txt.getText()); //Interest rate
double rate = rate1/100;
int term = Integer.parseInt((String)term_txt.getText()); //Length of Loan in Years
double perMonthRate = rate/12; //Monthly interst rate
double termMonths = term*12; //Number of months over which loan is amortized

//************calculation formula*******************

double monthlyPayment = principal (perMonthRate/(1 - Math.pow((1 - perMonthRate),(-termMonths))));

//formatting variables
DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //currency
DecimalFormat currencyFormat = new DecimalFormat("\$###,###,##0.00");	//Sets the comma for the currency
DecimalFormat pf = new DecimalFormat("#,##0.00%"); //percentages
DecimalFormat mi = new DecimalFormat("#,##0.000%"); //percentages
payment_txt.setText(df.format(monthlyPayment));

//Amoritization variables
double interestPaid = 0; //Amount of interest paid on the loan
double monthlyPrincipal = 0; //Amount of principal in each monthly payment
double principalBalance = principal; //running total of principal after payment
int paymentNumber = 1; //Variable used to set payment number.

//This loop increments the term length in years.
for(int y = 1; y <= term; y++)
{ //start outer loop
displayArea.append(""); //Inserts a blank line

//This loop is used to calculate and display the payment schedule information.
for (int m = 0; m<12; m++)
{ //start inner loop
interestPaid = principalBalance*perMonthRate;
monthlyPrincipal = monthlyPayment-interestPaid;
principalBalance = principalBalance - monthlyPrincipal;
displayArea.append("Month " + paymentNumber +"\t\t" +
df.format(monthlyPrincipal) +"\t\t" +
df.format(interestPaid) + "\t\t" +
df.format(principalBalance) +"\n");
paymentNumber++;
displayArea.setCaretPosition(0);
}
}
}
public static void main(String[] arguments) {
new CalculatorWk4();
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Java Mortgage Calculator Problem

### #2 n8wxs

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

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

## Re: Java Mortgage Calculator Problem

Posted 02 August 2010 - 06:19 PM

your formula is wrong: principal is a double, not a method.

```...
public void Payment() {
double principal = Double.parseDouble(principal_txt.getText()); //Principal amount
...
double monthlyPayment = principal(perMonthRate / (1 - Math.pow((1 - perMonthRate), (-termMonths))));
...

```

I think you want

```double monthlyPayment = principal * (perMonthRate / (1 - Math.pow((1 - perMonthRate), (-termMonths))));

```

This post has been edited by n8wxs: 02 August 2010 - 06:27 PM

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12186
• Posts: 45,250
• Joined: 27-December 08

## Re: Java Mortgage Calculator Problem

Posted 02 August 2010 - 08:09 PM