# Mortgage calculator with GUI

• (2 Pages)
• 1
• 2

## 29 Replies - 2829 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=b4a75d552bb351c4448091cc65158f06&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pcornacchione

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

# Mortgage calculator with GUI

Posted 25 September 2010 - 06:09 PM

Okay folks, i'm back again. **sigh** Trying to get this thing to work with user input for the loan amount and three choices for the term/interest rate. Then it needs the full amortization for each of the three. This is the disaster i have so far. I'm hopelessly lost and confused. I'd appreciate help, pointed to a tutorial, or anything to make this clear!!

```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 **not sure how to implement here
DecimalFormat percent = new DecimalFormat("0.00%");     						//formats 2 decimal places and % sign **not sure how to implement here
DecimalFormat whole = new DecimalFormat("0");           						//formats to whole number **not sure how to implement here

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("Please enter desired Mortgage Amount (do not enter \$ or cents)",JLabel.LEFT);
JTextField principal_txt = new JTextField(10);

JPanel row3 = new JPanel(new GridLayout(1,2));

JPanel row5 = new JPanel(new GridLayout(1, 2));
JLabel monthlyPayment_label = new JLabel("Your monthly payment is", JLabel.LEFT);
JTextField monthlyPayment_txt = new JTextField(10);

JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 20));				//centers buttons and set spacing between them
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
JScrollPane scroll = new JScrollPane(displayArea);

// Menu Selection for Term & Rate

public MortgageTest() {

super ("Mortgage Calculator");
setSize(600, 250);															//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);

pane.add(row3);				//**i think this needs to be the menu but how to set here?
row3.setMaximumSize( new Dimension( 10000, row3.getMinimumSize().height));
row3.setBorder( rowborder);

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

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

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

length = 7;
interestRate = 5.35;
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);
}
length = 15;
interestRate = 5.5;
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);
}
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) {

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

length = 7;
interestRate = 5.35;
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
}

length = 15;
interestRate = 5.5;
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
}

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";
displayTextArea.setText(titles);
displayTextArea.setText(number+"\t\t"+money.format(monthlyPayment)+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
String s = Double.toString(number);
numberTextArea.setText(s);
String s = Double.toString(monthlyPayment);
monthlyPaymentTextArea.setText(s);
String s = Double.toString(balance);
balanceTextArea.setText(s);
String s = Double.toString(interestPaid);
interestPaidTextArea.setText(s);
}
}
}
public static void main(String[] args) {
Frame f = new MortgageTest();
f.show();

}
}
```

Is This A Good Question/Topic? 0

## Replies To: Mortgage calculator with GUI

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,160
• Joined: 27-December 08

## Re: Mortgage calculator with GUI

Posted 25 September 2010 - 10:30 PM

So what specifically are your problems or errors?

### #3 pcornacchione

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

## Re: Mortgage calculator with GUI

Posted 25 September 2010 - 11:03 PM

macosxnerd101, on 25 September 2010 - 09:30 PM, said:

So what specifically are your problems or errors?

For starters nothing works! LOL

--------------------Configuration: <Default>--------------------
C:\MortgageTest.java:121: cannot find symbol
symbol : variable row4
location: class MortgageTest
^
C:\MortgageTest.java:122: cannot find symbol
symbol : variable row4
location: class MortgageTest
^
C:\MortgageTest.java:123: cannot find symbol
symbol : variable row4
location: class MortgageTest
^
C:\MortgageTest.java:125: cannot find symbol
symbol : variable row4
location: class MortgageTest
row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
^
C:\MortgageTest.java:125: cannot find symbol
symbol : variable row4
location: class MortgageTest
row4.setMaximumSize( new Dimension( 10000, row4.getMinimumSize().height));
^
C:\MortgageTest.java:126: cannot find symbol
symbol : variable row4
location: class MortgageTest
row4.setBorder( rowborder);
^
C:\MortgageTest.java:128: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:129: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:130: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:156: cannot find symbol
symbol : variable chiocesJComboBox
location: class MortgageTest
^
C:\MortgageTest.java:195: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:204: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:213: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:228: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:239: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:250: cannot find symbol
location: class MortgageTest
^
C:\MortgageTest.java:262: cannot find symbol
symbol : variable displayTextArea
location: class MortgageTest
displayTextArea.setText(titles);
^
C:\MortgageTest.java:263: cannot find symbol
symbol : variable displayTextArea
location: class MortgageTest
displayTextArea.setText(number+"\t\t"+money.format(monthlyPayment)+"\t\t"+money.format(balance)+"\t\t"+money.format(interestPaid));
^
C:\MortgageTest.java:265: cannot find symbol
symbol : variable numberTextArea
location: class MortgageTest
numberTextArea.setText(s);
^
C:\MortgageTest.java:266: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(monthlyPayment);
^
C:\MortgageTest.java:267: cannot find symbol
symbol : variable monthlyPaymentTextArea
location: class MortgageTest
monthlyPaymentTextArea.setText(s);
^
C:\MortgageTest.java:268: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(balance);
^
C:\MortgageTest.java:269: cannot find symbol
symbol : variable balanceTextArea
location: class MortgageTest
balanceTextArea.setText(s);
^
C:\MortgageTest.java:270: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(interestPaid);
^
C:\MortgageTest.java:271: cannot find symbol
symbol : variable interestPaidTextArea
location: class MortgageTest
interestPaidTextArea.setText(s);
^
Note: C:\MortgageTest.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
25 errors

Process completed.

--------------------Configuration: <Default>--------------------
java.lang.NoClassDefFoundError: MortgageTest
Caused by: java.lang.ClassNotFoundException: MortgageTest
at java.security.AccessController.doPrivileged(Native Method)
Could not find the main class: MortgageTest. Program will exit.
Process completed.

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,160
• Joined: 27-December 08

## Re: Mortgage calculator with GUI

Posted 25 September 2010 - 11:11 PM

After glancing over the errors, you didn't declare some of those variables in your class, so they cannot be found b/c they don't exist.

### #5 pcornacchione

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

## Re: Mortgage calculator with GUI

Posted 26 September 2010 - 10:12 AM

macosxnerd101, on 25 September 2010 - 10:11 PM, said:

After glancing over the errors, you didn't declare some of those variables in your class, so they cannot be found b/c they don't exist.

I thought i had everything declared? Can you be more specific please? I'm so braindead from fooling with this that i cannot see clearly. I know its prolly obvious to you but not to me!

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,160
• Joined: 27-December 08

## Re: Mortgage calculator with GUI

Posted 26 September 2010 - 11:26 AM

Four such variables are row4, radioButtonOne, radioButtonTwo, and radioButtonThree. If you use a crtl+f for these variables, you will see the first occurrences of these in use, but you will never find them declared anywhere in your code. This is an easy way to help you debug for the other occurrences of this error.

### #7 pcornacchione

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

## Re: Mortgage calculator with GUI

Posted 26 September 2010 - 11:53 AM

Okay, i'm confused now. How do you declare them? I don't remember ever having to declare things like that in previous programs. And then my next question is why only row4? Why not the other rows? I'm hopelessly confused now!

Me -->

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,160
• Joined: 27-December 08

## Re: Mortgage calculator with GUI

Posted 26 September 2010 - 12:03 PM

The row4 variable is the only one you didn't declare and initialize. Take a look at row1 JPanel row1 = new JPanel(); . There is no such declaration and initialization for row4, or the other variables I mentioned.

Below I explain declaration and initialization with a String as an example. See my comments for explanation.
```//declare x; note that x is null
//so that means you have a pointer
//that doesn't reference an Object in memory.
//This means you cannot use this variable
//besides comparing it to null
String x;

//initialize x to some value
//now you can use this String object
//and it's methods b/c x references
//"123" in memory
x = "123";

```

### #9 pcornacchione

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

## Re: Mortgage calculator with GUI

Posted 26 September 2010 - 01:09 PM

macosxnerd101, on 26 September 2010 - 11:03 AM, said:

The row4 variable is the only one you didn't declare and initialize. Take a look at row1 JPanel row1 = new JPanel(); . There is no such declaration and initialization for row4, or the other variables I mentioned.

Below I explain declaration and initialization with a String as an example. See my comments for explanation.
```//declare x; note that x is null
//so that means you have a pointer
//that doesn't reference an Object in memory.
//This means you cannot use this variable
//besides comparing it to null
String x;

//initialize x to some value
//now you can use this String object
//and it's methods b/c x references
//"123" in memory
x = "123";

```

Aahhh, it is slowly starting to make sense. I see now where i was not consistent with my naming and that threw me off with the declarations!!

I'm down to the following errors now....suggestions on how to fix them?

--------------------Configuration: <Default>--------------------
C:\MortgageTest.java:274: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(monthlyPayment);
^
C:\MortgageTest.java:276: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(balance);
^
C:\MortgageTest.java:278: s is already defined in actionPerformed(java.awt.event.ActionEvent)
String s = Double.toString(interestPaid);
^

### #10 k1ngcor3y

Reputation: 5
• Posts: 69
• Joined: 27-August 10

## Re: Mortgage calculator with GUI

Posted 26 September 2010 - 08:31 PM

Those errors are because you are declaring a variable more than once
```int x = 1;
int x = 2;

```

^^That is an error.
vv This is correct vv

```int x = 1;
x = 2;

```

You only need to declare a variable once

This post has been edited by k1ngcor3y: 26 September 2010 - 08:33 PM

### #11 pcornacchione

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

## Re: Mortgage calculator with GUI

Posted 26 September 2010 - 09:30 PM

Okay thank you. Got that much cleared up. But things still aren't working right but i'm gonna work on it some more and if i'm still screwed up i'll be back! LOL

Thank you to everyone for the help so far!! Y

### #12 pcornacchione

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

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 12:25 AM

**sigh** Yet again i'm here seeking wisdom....GUI looks great now. However, my radio buttons are not working properly which makes my math (assuming it will work) not work.
OH! And currently no errors...that's the only good thing.

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

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

length = 7;
interestRate = 5.35;
}

length = 15;
interestRate = 5.5;
}

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

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

length = 7;
interestRate = 5.35;
}

length = 15;
interestRate = 5.5;
}

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 MortgageTest();
f.show();

}
}
```

### #13 Dogstopper

Reputation: 2950
• Posts: 11,217
• Joined: 15-July 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 04:33 AM

You need to use a ButtonGroup:

It will group the buttons together so that you can only select one RadioButton in the group.

### #14 pcornacchione

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

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:52 AM

Okay, i created the button group. Double checked that i had all my names the same throughout the program. And now i get more errors. I'm doing the work. I'm following what everyone says to do. BUT i'm getting more and more confused because each time i make the change that is supposed to fix my problem i get more errors that send me back to square one! I'm not asking for anyone to do my work but can someone please tell me specifically what line the *bleep* problem is on? I'm frustrated and confused. Errors follow coding.

```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));
ButtonGroup pickOneGroup = new ButtonGroup();										//creates group for buttons

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

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

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

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 MortgageTest();
f.show();

}
}
```

--------------------Configuration: <Default>--------------------
C:\Users\Pam\Documents\MortgageTest.java:60: <identifier> expected
^
C:\Users\Pam\Documents\MortgageTest.java:60: <identifier> expected
^
C:\Users\Pam\Documents\MortgageTest.java:61: <identifier> expected
^
C:\Users\Pam\Documents\MortgageTest.java:61: <identifier> expected
^
C:\Users\Pam\Documents\MortgageTest.java:62: <identifier> expected
^
C:\Users\Pam\Documents\MortgageTest.java:62: <identifier> expected
^
6 errors

Process completed.
--------------------Configuration: <Default>--------------------
java.lang.NoClassDefFoundError: MortgageTest
Caused by: java.lang.ClassNotFoundException: MortgageTest
at java.security.AccessController.doPrivileged(Native Method)
Could not find the main class: MortgageTest. Program will exit.
Process completed.

### #15 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,160
• Joined: 27-December 08

## Re: Mortgage calculator with GUI

Posted 27 September 2010 - 09:59 AM

You can't add the JRadioButtons to the ButtonGroup outside of a method. You can initialize variables when you declare them outside of the constructor, but all other method calls must be in other methods.