2 Replies - 788 Views - Last Post: 30 August 2009 - 09:45 AM Rate Topic: -----

#1 rrspen316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-August 09

getting error cannot find symbol in line 211

Post icon  Posted 30 August 2009 - 07:53 AM

This is for change request 6 where I have to take either the input from a user for the amount, interest, and years or take it from 3 different loans. I just built on my last program and added to the GUI, but when I try to compile it, I am getting cannot find symbol for interest and years. I am getting 3 errors and they are all in the same line. I don't have to turn this in until Friday, but decided to start working on it. Could someone please help me. Also, I am new at this so if I do something wrong please let me know? Here is my code:

// import of the packages
import java.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RSpencerMortgageCalculatorV2
{
	public static void main (String[] args)
	{
		JFrame window = new MortCalculation();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setVisible(true);
	}
} // end of class

class MortCalculation extends JFrame implements ActionListener
{
	//delcare function variables
	protected JButton button7, button15, button30, buttonClear, buttonQuit, buttonCalc;
	protected JLabel labelMonthlyPay, labelAmount, labelTitle, labelAmortTable, labelTerm, labelRate;
	protected JTextField fieldPayment, fieldamount, fieldyears, fieldinterest;
	protected JTextArea areaAmortTable;

	// declare variables and array and initialize the values
	double InterestPaid, PrinciplePaid, Balance, monthlyPay, loanAmount;
	int[] loanYears = {7,15,30};
	double[] intRate = {5.35, 5.50, 5.75};
	double loanAmt = loanAmount;


	public void actionPerformed (ActionEvent e)
	{
		// perform based on which button was pressed
		if ("Calculate7".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment7();

			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");

			// need to set loan amount to the value from the field first which will need to be done for each loan calculations
			loanAmt = Double.parseDouble(fieldamount.getText());

			for(int x = 1; x <=(loanYears[0]*12); x++) // loop through all monthly payment for each loan
			{
				Balance=loanAmt; // set balance to loan amount

				// calculations on monthly payment
				InterestPaid = (((intRate[0]/100.0)/12.0)*Balance); // monthly interest paid
				PrinciplePaid = (paymentAmount-InterestPaid); // applied towards principal
				loanAmt=(Balance-PrinciplePaid); // loan balance after payment

				areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for
		} // end first if for button pressed

		else if ("Calculate15".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment15();

			fieldPayment.setText("" + (currency.format(paymentAmount)));

			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
			loanAmt = Double.parseDouble(fieldamount.getText());

			for (int x = 1; x <=(loanYears[1]*12); x++) // loop through all monthly payment for each loan
			{
				Balance = loanAmt; // set balance to loan amount

				// calculations for monthly payment
				InterestPaid=((intRate[1]/100/12)*Balance);
				PrinciplePaid=(paymentAmount-InterestPaid);
				loanAmt=((Balance-PrinciplePaid));

				areaAmortTable.append(x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for loop
		} // end else if

		else if ("Calculate30".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment30();
			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
			loanAmt = Double.parseDouble(fieldamount.getText());

			for(int x = 1; x <=(loanYears[2]*12); x++) // loop through all monthly payment for each loan
			{
				Balance = loanAmt; // set balance to loan amt

				// calculations for monthly payment
				InterestPaid = ((intRate[2]/100/12)*Balance);
				PrinciplePaid = (paymentAmount-InterestPaid);
				loanAmt=((Balance-PrinciplePaid));

				areaAmortTable.append(x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for loop
		} // end else if

		else if("Calc".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = Calc();

			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");

			loanAmt = Double.parseDouble(fieldamount.getText());
			int years = Integer.parseInt(fieldyears.getText());
			double interest = Double.parseDouble(fieldinterest.getText());

			for(int x = 1; x <=(years*12); x++)
			{
				Balance=loanAmt;

				InterestPaid = (((interest/100.00)/12.0)*Balance);
				PrinciplePaid = (paymentAmount-InterestPaid);
				loanAmt = (Balance-PrinciplePaid);

				areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n");
			}
		}
		else if ("Clear".equals(e.getActionCommand()))
		{
			ClearFields(); // action command for reset button
		}
		else if ("Quit".equals(e.getActionCommand()))
		{
			System.exit(0); // action command for quit button
		}
	}

	public double CalculatePayment7()
	{
		// check for valid numbers
		try
		{
			// perform calculations if input is valid
			fieldPayment.setText("");
			double Payment = (loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)), loanYears[0]*12));
			return Payment;
		}

		catch(NumberFormatException event)
		{
			// display error message if not numeric input
			JOptionPane.showMessageDialog(null, " Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double CalculatePayment15()
	{
		try
		{
			fieldPayment.setText("");
			double Payment=(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)), loanYears[1]*12));
			return Payment;
		}

		catch (NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, " Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double CalculatePayment30()
	{
		try
		{
			fieldPayment.setText("");
			double Payment = (loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)), loanYears[2]*12));
			return Payment;
		}
		catch (NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, "Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double Calc()
	{
		try
		{
			fieldPayment.setText("");
			fieldyears.setText("");
			fieldinterest.setText("");
			double Payment = (loanAmount() * (interest/100/12)) / (1 - Math.pow(1/(1 + (interest/100/12)), years*12));
			return Payment;
		}

		catch(NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, "Enter Number Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public void ClearFields()
	{
		// set all text fields to blank
		fieldPayment.setText ("");
		fieldamount.setText ("");
		areaAmortTable.setText("");
		fieldyears.setText("");
		fieldinterest.setText("");
	}

	public MortCalculation()
	{
		// set up and initialize buttons
		button7 = new JButton("7 years at 5.35%");
		button7.setActionCommand("Calculate7");
		button15 = new JButton("15 years at 5.5%");
		button15.setActionCommand("Calculate15");
		button30 = new JButton("30 years at 5.75%");
		button30.setActionCommand ("Calculate30");
		buttonCalc = new JButton("Calculate");
		buttonCalc.setActionCommand("Calculate");
		buttonClear = new JButton("Reset");
		buttonClear.setActionCommand("Clear");
		buttonQuit = new JButton("Quit");
		buttonQuit.setActionCommand("Quit");

		// set up labels and text field sizes
		labelTitle = new JLabel ("Mortgage Calculator");

		labelMonthlyPay = new JLabel("Monthly Payment");
		labelAmount = new JLabel("Amount");
		fieldPayment = new JTextField("", 12);
		fieldamount = new JTextField("", 10);
		labelTerm = new JLabel("Years");
		labelRate = new JLabel("Interest Rate");
		fieldyears = new JTextField("", 12);
		fieldinterest = new JTextField("", 12);
		labelAmortTable = new JLabel("Amortization Table");
		areaAmortTable = new JTextArea(10, 300);

		JScrollPane scrollPane = new JScrollPane(areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		// set up actionlisteners for each button
		button7.addActionListener(this);
		button15.addActionListener(this);
		button30.addActionListener(this);
		buttonCalc.addActionListener(this);
		buttonClear.addActionListener(this);
		buttonQuit.addActionListener(this);

		// construct gui and layout
		JPanel mypanel = new JPanel();
		mypanel.setLayout(null);

		mypanel.add(labelTitle);
		labelTitle.setBounds(150, 20, 500, 15);

		mypanel.add(labelAmount);
		labelAmount.setBounds(130, 60, 80, 25);

		mypanel.add(fieldamount);
		fieldamount.setBounds(240, 60, 100, 25);

		mypanel.add(labelTerm);
		labelTerm.setBounds(130, 85, 80, 40);

		mypanel.add(fieldyears);
		fieldyears.setBounds(240, 90, 100, 25);

		mypanel.add(labelRate);
		labelRate.setBounds(130, 125, 80, 25);

		mypanel.add(fieldinterest);
		fieldinterest.setBounds(240, 125, 100, 25);

		mypanel.add(button7);
		button7.setBounds(35, 170, 130, 30);

		mypanel.add(button15);
		button15.setBounds(180, 170, 130, 30);

		mypanel.add(button30);
		button30.setBounds(320, 170, 135, 30);

		mypanel.add(labelMonthlyPay);
		labelMonthlyPay.setBounds(130, 220, 100, 25);

		mypanel.add(fieldPayment);
		fieldPayment.setBounds(240, 220, 100, 25);
		fieldPayment.setEditable(false);

		mypanel.add(labelAmortTable);
		labelAmortTable.setBounds(180, 250, 300, 25);

		// add scrollpane, set bounds and set table as not editable
		// table is part of scrollpane now, so add and place only the scrollpane
		mypanel.add(scrollPane);
		scrollPane.setBounds(50, 280, 400, 270);
		areaAmortTable.setEditable(false);

		mypanel.add(buttonCalc);
		buttonCalc.setBounds(50, 570, 100, 30);

		mypanel.add(buttonClear);
		buttonClear.setBounds(170, 570, 95, 30);

		mypanel.add(buttonQuit);
		buttonQuit.setBounds(290, 570, 95, 30);

		this.setContentPane(mypanel);
		this.pack();
		this.setTitle("Mortgage Calculator");

		// set window size
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(500, 700);
	}

	public double loanAmount()
	{
		double loanAmount = Double.parseDouble(fieldamount.getText());
		return loanAmount;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: getting error cannot find symbol in line 211

#2 rrspen316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-August 09

Re: getting error cannot find symbol in line 211

Posted 30 August 2009 - 09:22 AM

I got past the error and it compiles fine now but I cannot get it to do anything when I press the Calculate button. Here is what I have so far. I am still looking it over but any help would be very much appreciated:

// import of the packages
import java.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RSpencerMortgageCalculatorV2
{
	public static void main (String[] args)
	{
		JFrame window = new MortCalculation();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setVisible(true);
	}
} // end of class

class MortCalculation extends JFrame implements ActionListener
{
	//delcare function variables
	protected JButton button7, button15, button30, buttonClear, buttonQuit, buttonCalc;
	protected JLabel labelMonthlyPay, labelAmount, labelTitle, labelAmortTable, labelTerm, labelRate;
	protected JTextField fieldPayment, fieldamount, fieldyears, fieldinterest;
	protected JTextArea areaAmortTable;

	// declare variables and array and initialize the values
	double InterestPaid, PrinciplePaid, Balance, monthlyPay, loanAmount, interest;
	int[] loanYears = {7,15,30};
	double[] intRate = {5.35, 5.50, 5.75};
	double loanAmt = loanAmount;
	int years;


	public void actionPerformed (ActionEvent e)
	{
		// perform based on which button was pressed
		if ("Calculate7".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment7();

			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");

			// need to set loan amount to the value from the field first which will need to be done for each loan calculations
			loanAmt = Double.parseDouble(fieldamount.getText());

			for(int x = 1; x <=(loanYears[0]*12); x++) // loop through all monthly payment for each loan
			{
				Balance=loanAmt; // set balance to loan amount

				// calculations on monthly payment
				InterestPaid = (((intRate[0]/100.0)/12.0)*Balance); // monthly interest paid
				PrinciplePaid = (paymentAmount-InterestPaid); // applied towards principal
				loanAmt=(Balance-PrinciplePaid); // loan balance after payment

				areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for
		} // end first if for button pressed

		else if ("Calculate15".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment15();

			fieldPayment.setText("" + (currency.format(paymentAmount)));

			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
			loanAmt = Double.parseDouble(fieldamount.getText());

			for (int x = 1; x <=(loanYears[1]*12); x++) // loop through all monthly payment for each loan
			{
				Balance = loanAmt; // set balance to loan amount

				// calculations for monthly payment
				InterestPaid=((intRate[1]/100/12)*Balance);
				PrinciplePaid=(paymentAmount-InterestPaid);
				loanAmt=((Balance-PrinciplePaid));

				areaAmortTable.append(x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for loop
		} // end else if

		else if ("Calculate30".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment30();
			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
			loanAmt = Double.parseDouble(fieldamount.getText());

			for(int x = 1; x <=(loanYears[2]*12); x++) // loop through all monthly payment for each loan
			{
				Balance = loanAmt; // set balance to loan amt

				// calculations for monthly payment
				InterestPaid = ((intRate[2]/100/12)*Balance);
				PrinciplePaid = (paymentAmount-InterestPaid);
				loanAmt=((Balance-PrinciplePaid));

				areaAmortTable.append(x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for loop
		} // end else if

		else if("Calc".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = Calc();

			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");

			loanAmt = Double.parseDouble(fieldamount.getText());
			int years = Integer.parseInt(fieldyears.getText());
			double interest = Double.parseDouble(fieldinterest.getText());

			for(int x = 1; x <=(years*12); x++)
			{
				Balance=loanAmt;

				InterestPaid = (((interest/100.00)/12.0)*Balance);
				PrinciplePaid = (paymentAmount-InterestPaid);
				loanAmt = (Balance-PrinciplePaid);

				areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n");
			}
		}
		else if ("Clear".equals(e.getActionCommand()))
		{
			ClearFields(); // action command for reset button
		}
		else if ("Quit".equals(e.getActionCommand()))
		{
			System.exit(0); // action command for quit button
		}
	}

	public double CalculatePayment7()
	{
		// check for valid numbers
		try
		{
			// perform calculations if input is valid
			fieldPayment.setText("");
			double Payment = (loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)), loanYears[0]*12));
			return Payment;
		}

		catch(NumberFormatException event)
		{
			// display error message if not numeric input
			JOptionPane.showMessageDialog(null, " Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double CalculatePayment15()
	{
		try
		{
			fieldPayment.setText("");
			double Payment=(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)), loanYears[1]*12));
			return Payment;
		}

		catch (NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, " Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double CalculatePayment30()
	{
		try
		{
			fieldPayment.setText("");
			double Payment = (loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)), loanYears[2]*12));
			return Payment;
		}
		catch (NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, "Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double Calc()
	{
		try
		{
			fieldPayment.setText("");

			double Payment = (loanAmount() * (interest/100/12)) / (1 - Math.pow(1/(1 + (interest/100/12)), years*12));
			return Payment;
		}

		catch(NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, "Enter Number Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public void ClearFields()
	{
		// set all text fields to blank
		fieldPayment.setText ("");
		fieldamount.setText ("");
		areaAmortTable.setText("");
		fieldyears.setText("");
		fieldinterest.setText("");
	}

	public MortCalculation()
	{
		// set up and initialize buttons
		button7 = new JButton("7 years at 5.35%");
		button7.setActionCommand("Calculate7");
		button15 = new JButton("15 years at 5.5%");
		button15.setActionCommand("Calculate15");
		button30 = new JButton("30 years at 5.75%");
		button30.setActionCommand ("Calculate30");
		buttonCalc = new JButton("Calculate");
		buttonCalc.setActionCommand("Calculate");
		buttonClear = new JButton("Reset");
		buttonClear.setActionCommand("Clear");
		buttonQuit = new JButton("Quit");
		buttonQuit.setActionCommand("Quit");

		// set up labels and text field sizes
		labelTitle = new JLabel ("Mortgage Calculator");

		labelMonthlyPay = new JLabel("Monthly Payment");
		labelAmount = new JLabel("Amount");
		fieldPayment = new JTextField("", 12);
		fieldamount = new JTextField("", 10);
		labelTerm = new JLabel("Years");
		labelRate = new JLabel("Interest Rate");
		fieldyears = new JTextField("", 12);
		fieldinterest = new JTextField("", 12);
		labelAmortTable = new JLabel("Amortization Table");
		areaAmortTable = new JTextArea(10, 300);

		JScrollPane scrollPane = new JScrollPane(areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		// set up actionlisteners for each button
		button7.addActionListener(this);
		button15.addActionListener(this);
		button30.addActionListener(this);
		buttonCalc.addActionListener(this);
		buttonClear.addActionListener(this);
		buttonQuit.addActionListener(this);

		// construct gui and layout
		JPanel mypanel = new JPanel();
		mypanel.setLayout(null);

		mypanel.add(labelTitle);
		labelTitle.setBounds(150, 20, 500, 15);

		mypanel.add(labelAmount);
		labelAmount.setBounds(130, 60, 80, 25);

		mypanel.add(fieldamount);
		fieldamount.setBounds(240, 60, 100, 25);

		mypanel.add(labelTerm);
		labelTerm.setBounds(130, 85, 80, 40);

		mypanel.add(fieldyears);
		fieldyears.setBounds(240, 90, 100, 25);

		mypanel.add(labelRate);
		labelRate.setBounds(130, 125, 80, 25);

		mypanel.add(fieldinterest);
		fieldinterest.setBounds(240, 125, 100, 25);

		mypanel.add(button7);
		button7.setBounds(35, 170, 130, 30);

		mypanel.add(button15);
		button15.setBounds(180, 170, 130, 30);

		mypanel.add(button30);
		button30.setBounds(320, 170, 135, 30);

		mypanel.add(labelMonthlyPay);
		labelMonthlyPay.setBounds(130, 220, 100, 25);

		mypanel.add(fieldPayment);
		fieldPayment.setBounds(240, 220, 100, 25);
		fieldPayment.setEditable(false);

		mypanel.add(labelAmortTable);
		labelAmortTable.setBounds(180, 250, 300, 25);

		// add scrollpane, set bounds and set table as not editable
		// table is part of scrollpane now, so add and place only the scrollpane
		mypanel.add(scrollPane);
		scrollPane.setBounds(50, 280, 400, 270);
		areaAmortTable.setEditable(false);

		mypanel.add(buttonCalc);
		buttonCalc.setBounds(50, 570, 100, 30);

		mypanel.add(buttonClear);
		buttonClear.setBounds(170, 570, 95, 30);

		mypanel.add(buttonQuit);
		buttonQuit.setBounds(290, 570, 95, 30);

		this.setContentPane(mypanel);
		this.pack();
		this.setTitle("Mortgage Calculator");

		// set window size
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(500, 700);
	}

	public double loanAmount()
	{
		double loanAmount = Double.parseDouble(fieldamount.getText());
		return loanAmount;
	}
}

This post has been edited by rrspen316: 30 August 2009 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 rrspen316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-August 09

Re: getting error cannot find symbol in line 211

Posted 30 August 2009 - 09:45 AM

Ok I got it to calculate when the button is pressed but it is putting square boxes in the amount field and the amort table for everything. I am not sure if I am suppose to add the code everytime I adjust it but here is what I have so far:

// import of the packages
import java.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RSpencerMortgageCalculatorV2
{
	public static void main (String[] args)
	{
		JFrame window = new MortCalculation();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setVisible(true);
	}
} // end of class

class MortCalculation extends JFrame implements ActionListener
{
	//delcare function variables
	protected JButton button7, button15, button30, buttonClear, buttonQuit, buttonCalc;
	protected JLabel labelMonthlyPay, labelAmount, labelTitle, labelAmortTable, labelTerm, labelRate;
	protected JTextField fieldPayment, fieldamount, fieldyears, fieldinterest;
	protected JTextArea areaAmortTable;

	// declare variables and array and initialize the values
	double InterestPaid, PrinciplePaid, Balance, monthlyPay, loanAmount, interest;
	int[] loanYears = {7,15,30};
	double[] intRate = {5.35, 5.50, 5.75};
	double loanAmt = loanAmount;
	int years;


	public void actionPerformed (ActionEvent e)
	{
		// perform based on which button was pressed
		if ("Calculate7".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment7();

			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");

			// need to set loan amount to the value from the field first which will need to be done for each loan calculations
			loanAmt = Double.parseDouble(fieldamount.getText());

			for(int x = 1; x <=(loanYears[0]*12); x++) // loop through all monthly payment for each loan
			{
				Balance=loanAmt; // set balance to loan amount

				// calculations on monthly payment
				InterestPaid = (((intRate[0]/100.0)/12.0)*Balance); // monthly interest paid
				PrinciplePaid = (paymentAmount-InterestPaid); // applied towards principal
				loanAmt=(Balance-PrinciplePaid); // loan balance after payment

				areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for
		} // end first if for button pressed

		else if ("Calculate15".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment15();

			fieldPayment.setText("" + (currency.format(paymentAmount)));

			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
			loanAmt = Double.parseDouble(fieldamount.getText());

			for (int x = 1; x <=(loanYears[1]*12); x++) // loop through all monthly payment for each loan
			{
				Balance = loanAmt; // set balance to loan amount

				// calculations for monthly payment
				InterestPaid=((intRate[1]/100/12)*Balance);
				PrinciplePaid=(paymentAmount-InterestPaid);
				loanAmt=((Balance-PrinciplePaid));

				areaAmortTable.append(x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for loop
		} // end else if

		else if ("Calculate30".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = CalculatePayment30();
			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
			loanAmt = Double.parseDouble(fieldamount.getText());

			for(int x = 1; x <=(loanYears[2]*12); x++) // loop through all monthly payment for each loan
			{
				Balance = loanAmt; // set balance to loan amt

				// calculations for monthly payment
				InterestPaid = ((intRate[2]/100/12)*Balance);
				PrinciplePaid = (paymentAmount-InterestPaid);
				loanAmt=((Balance-PrinciplePaid));

				areaAmortTable.append(x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n"); // add to amort table
			} // end for loop
		} // end else if

		else if("Calc".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double paymentAmount = Calc();

			fieldPayment.setText("" + (currency.format(paymentAmount)));
			areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");

			loanAmt = Double.parseDouble(fieldamount.getText());
			int years = Integer.parseInt(fieldyears.getText());
			double interest = Double.parseDouble(fieldinterest.getText());

			for(int x = 1; x <=(years*12); x++)
			{
				Balance=loanAmt;

				InterestPaid = (((interest/100.00)/12.0)*Balance);
				PrinciplePaid = (paymentAmount-InterestPaid);
				loanAmt = (Balance-PrinciplePaid);

				areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid) + "\n");
			}
		}
		else if ("Clear".equals(e.getActionCommand()))
		{
			ClearFields(); // action command for reset button
		}
		else if ("Quit".equals(e.getActionCommand()))
		{
			System.exit(0); // action command for quit button
		}
	}

	public double CalculatePayment7()
	{
		// check for valid numbers
		try
		{
			// perform calculations if input is valid
			fieldPayment.setText("");
			double Payment = (loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)), loanYears[0]*12));
			return Payment;
		}

		catch(NumberFormatException event)
		{
			// display error message if not numeric input
			JOptionPane.showMessageDialog(null, " Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double CalculatePayment15()
	{
		try
		{
			fieldPayment.setText("");
			double Payment=(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)), loanYears[1]*12));
			return Payment;
		}

		catch (NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, " Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double CalculatePayment30()
	{
		try
		{
			fieldPayment.setText("");
			double Payment = (loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)), loanYears[2]*12));
			return Payment;
		}
		catch (NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, "Enter Numeric Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public double Calc()
	{
		try
		{
			fieldPayment.setText("");

			double Payment = (loanAmount() * (interest/100/12)) / (1 - Math.pow(1/(1 + (interest/100/12)), years*12));
			return Payment;
		}

		catch(NumberFormatException event)
		{
			JOptionPane.showMessageDialog(null, "Enter Number Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
			return 0;
		}
	}

	public void ClearFields()
	{
		// set all text fields to blank
		fieldPayment.setText ("");
		fieldamount.setText ("");
		areaAmortTable.setText("");
		fieldyears.setText("");
		fieldinterest.setText("");
	}

	public MortCalculation()
	{
		// set up and initialize buttons
		button7 = new JButton("7 years at 5.35%");
		button7.setActionCommand("Calculate7");
		button15 = new JButton("15 years at 5.5%");
		button15.setActionCommand("Calculate15");
		button30 = new JButton("30 years at 5.75%");
		button30.setActionCommand ("Calculate30");
		buttonCalc = new JButton("Calculate");
		buttonCalc.setActionCommand("Calc");
		buttonClear = new JButton("Reset");
		buttonClear.setActionCommand("Clear");
		buttonQuit = new JButton("Quit");
		buttonQuit.setActionCommand("Quit");

		// set up labels and text field sizes
		labelTitle = new JLabel ("Mortgage Calculator");

		labelMonthlyPay = new JLabel("Monthly Payment");
		labelAmount = new JLabel("Amount");
		fieldPayment = new JTextField("", 12);
		fieldamount = new JTextField("", 10);
		labelTerm = new JLabel("Years");
		labelRate = new JLabel("Interest Rate");
		fieldyears = new JTextField("", 12);
		fieldinterest = new JTextField("", 12);
		labelAmortTable = new JLabel("Amortization Table");
		areaAmortTable = new JTextArea(10, 300);

		JScrollPane scrollPane = new JScrollPane(areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		// set up actionlisteners for each button
		button7.addActionListener(this);
		button15.addActionListener(this);
		button30.addActionListener(this);
		buttonCalc.addActionListener(this);
		buttonClear.addActionListener(this);
		buttonQuit.addActionListener(this);

		// construct gui and layout
		JPanel mypanel = new JPanel();
		mypanel.setLayout(null);

		mypanel.add(labelTitle);
		labelTitle.setBounds(150, 20, 500, 15);

		mypanel.add(labelAmount);
		labelAmount.setBounds(130, 60, 80, 25);

		mypanel.add(fieldamount);
		fieldamount.setBounds(240, 60, 100, 25);

		mypanel.add(labelTerm);
		labelTerm.setBounds(130, 85, 80, 40);

		mypanel.add(fieldyears);
		fieldyears.setBounds(240, 90, 100, 25);

		mypanel.add(labelRate);
		labelRate.setBounds(130, 125, 80, 25);

		mypanel.add(fieldinterest);
		fieldinterest.setBounds(240, 125, 100, 25);

		mypanel.add(button7);
		button7.setBounds(35, 170, 130, 30);

		mypanel.add(button15);
		button15.setBounds(180, 170, 130, 30);

		mypanel.add(button30);
		button30.setBounds(320, 170, 135, 30);

		mypanel.add(labelMonthlyPay);
		labelMonthlyPay.setBounds(130, 220, 100, 25);

		mypanel.add(fieldPayment);
		fieldPayment.setBounds(240, 220, 100, 25);
		fieldPayment.setEditable(false);

		mypanel.add(labelAmortTable);
		labelAmortTable.setBounds(180, 250, 300, 25);

		// add scrollpane, set bounds and set table as not editable
		// table is part of scrollpane now, so add and place only the scrollpane
		mypanel.add(scrollPane);
		scrollPane.setBounds(50, 280, 400, 270);
		areaAmortTable.setEditable(false);

		mypanel.add(buttonCalc);
		buttonCalc.setBounds(50, 570, 100, 30);

		mypanel.add(buttonClear);
		buttonClear.setBounds(170, 570, 95, 30);

		mypanel.add(buttonQuit);
		buttonQuit.setBounds(290, 570, 95, 30);

		this.setContentPane(mypanel);
		this.pack();
		this.setTitle("Mortgage Calculator");

		// set window size
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(500, 700);
	}

	public double loanAmount()
	{
		double loanAmount = Double.parseDouble(fieldamount.getText());
		return loanAmount;
	}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1