I am having multible problems with my code. Here is what it is supposed to do: 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. Here is the code.

/*Week 2 assignment
author: Chris Butler
Date: April 3, 2010
*/

import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.JTextField.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class PaymentCalculatorwk2 extends JFrame implements ActionListener
{
	//Using Decimal format to display money
	DecimalFormat money = new DecimalFormat ("$0,000.00");

	//Panel for user input
	JPanel firstRow = new JPanel();
	JPanel secondRow = new JPanel();
	JPanel thirdRow = new JPanel();
	JPanel fourthRow = new JPanel();


	//Panel for Buttons
	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();

	//Labels with descriptions
	JLabel loanLabel = new JLabel("Principle amount");
	JTextField p = new JTextField(8);
	JLabel YearLabel = JLabel("Year amount");
	JTextField p = new JTextField(6);
    JLabel rateLabel = new JLabel("Interest Rate");
    JTextField p = new JTextField(6);
    JLabel paymentLabel = new JLabel("Monthly Payment");
    JLabel displaypayment = new JLabel(6);

    //Create Buttons
    JButton calculateButton = new JButton("Payment");
    JButton exitButton = new JButton("Exit");

    public static void main(String[] args)
    {




		PaymentCalculatorwk2 f = new PaymentCalculatorwk2();

		f.setSize(450,350);
		f.setTitle("Mortgage Calculator");
		f.setLacation(300,350);
		f.setVisible(true);
    }

		public PaymentCalculatorwk2()
		{
			Container c = getContentPane();
			c.setLayout((new BorderLayout()));
			fieldPanel.setLayout(new GridLayout(8,1));
			FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
			firstRow.setLayout(rowSetup);
			secondRow.setLayout(rowSetup);
			thirdRow.setLayout(rowSetup);
			fourthRow.setLayout(rowSetup);
		    buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));


		    //add fields to rows
			firstRow.add(loanLabel);
			firstRow.add(p);
		    secondRow.add(termLabel);
			secondRow.add(t);
			thirdRow.add(rateLabel);
			thirdRow.add(r);
			fourthRow.add(paymentLabel);
			fourthRow.add(displayPayment);

			//Add rows to panel
			fieldPanel.add(firstRow);
			fieldPanel.add(secondRow);
			fieldPanel.add(thirdRow);
			fieldPanel.add(fourthRow);

			//add button to panel
			buttonPanel.add(calculateButton);
			buttonPanel.add(quitButton);

			//Add panels to frame
			c.add(fieldPanel, BorderLayout.CENTER);
			c.add(buttonPanel, BorderLayout.SOUTH);

			//add fuctionality to the button
			calculateButton.addActionListener(this);
			quitButton.addActionListener(this);

	     }

	     public void actionPerformed(ActionEvent e)
         {
			 //Calculations for user input
			 PaymentCalculator();
	     }
	     	 public void PaymentCalculator()
	     	 {
				 //Calculate monthly payment of loan
				 double principle = 0;			                    // Principle Loan Amount
				 double AnnualInterest = 0;	                        // Annual Interest Amaount
				 int Months = 0;				                        // Number of Months
				 double MonthlyInterest= AnnualInterest/(12*100);   // Monthly interest
				 double MonthlyPayment = 0;                         // Monthly Payment
				 double CurrentMonthlyInterest = 0;                 // Current Monthly Interest
				 double PaidPerMonth = 0;                           // Amount paid per month
                double NewBalance = 0;                              // monthly Balance
                MonthlyPayment = principle * (MonthlyInterest / (1 - (Math.pow(1 + MonthlyInterest, -Months))));
                String displayMonthlyPayment = Decimal.format (Payment);
                System.out.println(payment);
		     }
}

Here is the errors.
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:35: p is already defined in PaymentCalculatorwk2
JTextField p = new JTextField(6);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:37: p is already defined in PaymentCalculatorwk2
JTextField p = new JTextField(6);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:34: cannot find symbol
symbol : method JLabel(java.lang.String)
location: class PaymentCalculatorwk2
JLabel YearLabel = JLabel("Year amount");
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:39: cannot find symbol
symbol : constructor JLabel(int)
location: class javax.swing.JLabel
JLabel displaypayment = new JLabel(6);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:55: cannot find symbol
symbol : method setLacation(int,int)
location: class PaymentCalculatorwk2
f.setLacation(300,350);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:75: cannot find symbol
symbol : variable termLabel
location: class PaymentCalculatorwk2
secondRow.add(termLabel);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:76: cannot find symbol
symbol : variable t
location: class PaymentCalculatorwk2
secondRow.add(t);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:78: cannot find symbol
symbol : variable r
location: class PaymentCalculatorwk2
thirdRow.add(r);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:80: cannot find symbol
symbol : variable displayPayment
location: class PaymentCalculatorwk2
fourthRow.add(displayPayment);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:90: cannot find symbol
symbol : variable quitButton
location: class PaymentCalculatorwk2
buttonPanel.add(quitButton);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:98: cannot find symbol
symbol : variable quitButton
location: class PaymentCalculatorwk2
quitButton.addActionListener(this);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:119: cannot find symbol
symbol : variable Payment
location: class PaymentCalculatorwk2
String displayMonthlyPayment = Decimal.format (Payment);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:119: cannot find symbol
symbol : variable Decimal
location: class PaymentCalculatorwk2
String displayMonthlyPayment = Decimal.format (Payment);
^
F:\School\PRG421\week 1\PaymentCalculatorwk2.java:120: cannot find symbol
symbol : variable payment
location: class PaymentCalculatorwk2
System.out.println(payment);
^
14 errors

Tool completed with exit code 1
Please help.

Recommended Answers

All 5 Replies

you've got quite a number of typo's in there.
just the main things you should correct first:
a variable name has to be unique, so you can not have several JTextFields with the name 'p'
if you call your button 'exitButton', don't be surprised if the compiler does not know what you mean by quitButton.
JLabel does not have a constructor that takes an int as a single parameter.
your last lines, I think you mean MontlyPayment and displayMonthlyPayment over there

correct those, and I bet you'll have a lot of those errors removed

Thank you for your help. I went through everything again and got it to compile. However I can't get it to calculate. All I get for an answer is "NaN". Can I get some fresh eyes on this please.

/*
Week 2 assignment
author: Chris Butler
Date: April 3, 2010
*/

import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.JTextField.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class PaymentCalculatorwk2 extends JFrame implements ActionListener
{
	//Using Decimal format to display money
	DecimalFormat money = new DecimalFormat ("$0,000.00");

	//Panel for user input
	JPanel firstRow = new JPanel();
	JPanel secondRow = new JPanel();
	JPanel thirdRow = new JPanel();
	JPanel fourthRow = new JPanel();


	//Panel for Buttons
	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();

	//Labels with descriptions
	JLabel loanLabel = new JLabel("Principle amount");
	JTextField p = new JTextField(8);
	JLabel YearLabel = new JLabel("Year amount");
	JTextField Q = new JTextField(6);
    JLabel rateLabel = new JLabel("Interest Rate");
    JTextField R = new JTextField(6);
    JLabel paymentLabel = new JLabel("Monthly Payment");
    JLabel displaypayment = new JLabel();

    //Create Buttons
    JButton calculateButton = new JButton("Payment");
    JButton exitButton = new JButton("Exit");

    public static void main(String[] args)
    {




		PaymentCalculatorwk2 f = new PaymentCalculatorwk2();

		f.setSize(450,350);
		f.setTitle("Mortgage Calculator");
		f.setLocation(300,350);
		f.setVisible(true);
    }

		public PaymentCalculatorwk2()
		{
			Container c = getContentPane();
			c.setLayout((new BorderLayout()));
			fieldPanel.setLayout(new GridLayout(8,1));
			FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
			firstRow.setLayout(rowSetup);
			secondRow.setLayout(rowSetup);
			thirdRow.setLayout(rowSetup);
			fourthRow.setLayout(rowSetup);
		    buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));


		    //add fields to rows
			firstRow.add(loanLabel);
			firstRow.add(p);
		    secondRow.add(YearLabel);
			secondRow.add(Q);
			thirdRow.add(rateLabel);
			thirdRow.add(R);
			fourthRow.add(paymentLabel);
			fourthRow.add(displaypayment);

			//Add rows to panel
			fieldPanel.add(firstRow);
			fieldPanel.add(secondRow);
			fieldPanel.add(thirdRow);
			fieldPanel.add(fourthRow);

			//add button to panel
			buttonPanel.add(calculateButton);
			buttonPanel.add(exitButton);

			//Add panels to frame
			c.add(fieldPanel, BorderLayout.CENTER);
			c.add(buttonPanel, BorderLayout.SOUTH);

			//add fuctionality to the button
			calculateButton.addActionListener(this);
			exitButton.addActionListener(this);

	     }

	     public void actionPerformed(ActionEvent e)
         {
			 //Calculations for user input
			 PaymentCalculator();
	     }
	     	 public void PaymentCalculator()
	     	 {
				 //Calculate monthly payment of loan
				 double principle = 0;			                    // Principle Loan Amount
				 double AnnualInterest = 0;	                        // Annual Interest Amaount
				 int Months = 0;				                        // Number of Months
				 double MonthlyInterest= AnnualInterest/(12*100);   // Monthly interest
				 double MonthlyPayment = 0;                         // Monthly Payment
				 double CurrentMonthlyInterest = 0;                 // Current Monthly Interest
				 double PaidPerMonth = 0;                           // Amount paid per month
                double NewBalance = 0;                              // monthly Balance
                MonthlyPayment = principle * (MonthlyInterest / (1 - (Math.pow(1 + MonthlyInterest, -Months))));
                String displayMonthlyPayment = money.format (MonthlyPayment);
                paymentLabel.setText("" + MonthlyPayment);
		     }
}

NaN = Not a Number
propably something wrong in your calculation

I got the number to work. But I have a problem getting the exit button to work. Where am I going wrong?

/*
Week 2 assignment
author: Chris Butler
Date: April 3, 2010
*/

import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.JTextField.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class PaymentCalculatorwk2 extends JFrame implements ActionListener
{
	//Using Decimal format to display money
	DecimalFormat money = new DecimalFormat ("$0,000.00");

	//Panel for user input
	JPanel firstRow = new JPanel();
	JPanel secondRow = new JPanel();
	JPanel thirdRow = new JPanel();
	JPanel fourthRow = new JPanel();


	//Panel for Buttons
	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();

	//Labels with descriptions
	JLabel loanLabel = new JLabel("Principle amount");
	JTextField p = new JTextField(8);
	JLabel YearLabel = new JLabel("Year amount");
	JTextField Q = new JTextField(6);
    JLabel rateLabel = new JLabel("Interest Rate");
    JTextField R = new JTextField(6);
    JLabel paymentLabel = new JLabel("Monthly Payment");
    JLabel displaypayment = new JLabel();

    //Create Buttons
    JButton calculateButton = new JButton("Payment");
    JButton exitButton = new JButton("Exit");

    public static void main(String[] args)
    {




		PaymentCalculatorwk2 f = new PaymentCalculatorwk2();

		f.setSize(450,350);
		f.setTitle("Mortgage Calculator");
		f.setLocation(300,350);
		f.setVisible(true);
    }

		public PaymentCalculatorwk2()
		{
			Container c = getContentPane();
			c.setLayout((new BorderLayout()));
			fieldPanel.setLayout(new GridLayout(8,1));
			FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
			firstRow.setLayout(rowSetup);
			secondRow.setLayout(rowSetup);
			thirdRow.setLayout(rowSetup);
			fourthRow.setLayout(rowSetup);
		    buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));


		    //add fields to rows
			firstRow.add(loanLabel);
			firstRow.add(p);
		    secondRow.add(YearLabel);
			secondRow.add(Q);
			thirdRow.add(rateLabel);
			thirdRow.add(R);
			fourthRow.add(paymentLabel);
			fourthRow.add(displaypayment);

			//Add rows to panel
			fieldPanel.add(firstRow);
			fieldPanel.add(secondRow);
			fieldPanel.add(thirdRow);
			fieldPanel.add(fourthRow);

			//add button to panel
			buttonPanel.add(calculateButton);
			buttonPanel.add(exitButton);

			//Add panels to frame
			c.add(fieldPanel, BorderLayout.CENTER);
			c.add(buttonPanel, BorderLayout.SOUTH);

			//add fuctionality to the button
			calculateButton.addActionListener(this);
			exitButton.addActionListener(this);

	     }

	     public void actionPerformed(ActionEvent e)
         {
			 //Calculations for user input
			 PaymentCalculator();
	     }
	     	 public void PaymentCalculator()
	     	 {
				 //Calculate monthly payment of loan
				 double principle = Double.parseDouble(p.getText());			                    // Principle Loan Amount
				 double Months = Double.parseDouble(Q.getText());                                   // Number of years in months
				 double AnnualInterest = Double.parseDouble(R.getText());	                        // Annual Interest Amaount



                double MonthlyPayment = principle * (((AnnualInterest)/1200) / (1 - (Math.pow(1 + ((AnnualInterest)/1200), -(Months)))));
                String displayMonthlyPayment = money.format (MonthlyPayment);
                paymentLabel.setText("" + MonthlyPayment);
		     }
}

add a seperate (unique) event to your code, link it to your button, and enter this as body of the method:

System.exit(0);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.