Ok...The assignment is to create a mortgage calculator with a GUI that asks for the principle amount, has a drop down box that allows the user to select an interest rate and term (ie 7-yr at 5.35%) and displays the informat, payment, and an amortized payment schedule...I beat my head into a wall on this and I can't seem to get anywhere. I keep throwing allot of errors. This is the fourth varient I have done trying to find a way to make this work...any help would be greatly appreciated.

/**
*This JFrame is used to gather mortgage data from the user and display the monthly
*payment amount of the mortgage based on the input.
*
* @Title	MortCalc2
* @author	William Cook
* @Date		4/19/2009
* @version	1.1
*/

import java.util.*;
import java.text.NumberFormat;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MortCalc2r1
	extends JFrame
	implements ActionListener {

		//create labels and fields
		JLabel nameLabel = new JLabel("Hello. Please enter your name here.");
		JLabel principleLabel = new JLabel("Please enter your desired loan amount here.");
		JLabel ratestringLabel = new JLabel("Please select your rate and term.");
		JButton calcButton = new JButton("Calculate");
		JButton clearButton = new JButton("Clear Form");
		JButton exitButton = new JButton("Exit");
		JTextField nameTField = new JTextField ();
		JTextField principleTField = new JTextField ();
		JComboBox ratestring = newJComboBox(rateterm);

		//declare variables
		String [] rateterm={"7-year at 5.35%","15-year at 5.50%","30-year at 5.755"};
		String name = "unknown";
		String display = "0";
		double principle = 0;
		double payment;
		int month = 1;
		double pbalabce = 0;
		double ppaid = 0;
		double ipaid = 0;
		boolean fielderror = false;
		JTextArea displayArea = new JTextArea();

		private Container con = getContentPane();

		public static void main(String[] args) {
			MortCalc2r1 rta = new MortCalc2r1();
		}

			public MortCalc2r1() {
				try {
					jbInit();
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}

			private void jbInit() throws Exception {
				setSize(600,600);
				setTitle("William Cook's Personal Mortgage Calculator");
				con.setLayout(null);

				nameLabel.setLocation(20,20);
				nameLabel.setSize(400,20);

				nameTField.setLocation(450,20);
				nameTField.setSize(80,20);

				principleLabel.setLocation(20,60);
				principleLabel.setSize(400,20);

				principleTField.setLocation(450,60);
				principleTField.setSize(80,20);

				ratestringLabel.setLocation(20,100);
				ratestringLabel.setSize(400,20);

				calcButton.setLocation(20,200);
				calcButton.setSize(100,20);

				clearButton.setLocation(150,200);
				clearButton.setSize(100,20);

				exitButton.setLocation(280,200);
				exitButton.setSize(100,20);

				con.add(nameLabel, null);
				con.add(nameTField, null);
				con.add(principleLabel, null);
				con.add(principleTField, null);
				con.add(ratestringLabel, null);
				con.add(ratestring, null);
				con.add(calcButton, null);
				con.add(clearButton, null);
				con.add(exitButton, null);
				con.add(displayArea, null);

				ratestring.addActionListener(this);
				calcButton.addActionListener(this);
				clearButton.addActionListener(this);
				exitButton.addActionListener(this);

				setVisible(true);
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}

			public void actionPerformed(ActionEvent thisEvent) {

				Object source = thisEvent.getSource();

				if (thisEvent.getSource() instanceof JComboBox) {
					JComboBox ratestring = (JComboBox) thisEvent.getSource();
				}

				if (source == calcButton) {
					displaydata();
				}

				if (source == clearButton) {
					nameTField.setText("");
					principleTField.setText("");
					interestTField.setText("");
					termTField.setText("");
					displayArea.setText("");
				}

				if(source == exitButton) {
					System.exit(0);
				}
			}
			public void displaydata() {

				name = nameTField.getText();
					try {
				principle = Double.parseDouble(principleTField.getText());
				fielderror = false
				pblance = principle;
				if (ratestring == "7-year at 5.35%")
						{
							double interest = 0.0535;
							int term = 7;
							};

				else if(ratestring == "15-year at 5.50%")
						{
							double interest = 0.055;
							int term = 15;
							};

				else (display == "30-year at 5.755")
						{
							double interest = 0.0535;
							int term = 7;
							};

							payment = ((interest/12) * principle)/(1-(Math.pow(1+(interest/12), (term * -12))));

							pbalance = pbalance - ppaid;
							ppaid = payment - ipaid;
							ipaid = ((interest/12)*pbalance);
							month++;

					catch (Exception ex){
						fielderror = true;
					}
					if (!fielderror){
						while (pbalance > 0)
						String display = "Thank you for using the Personal Mortgage Calculator " + name + ", \n";
						display = display + "Your Principle balance is: "+ NumberFormat.getCurrencyInstance().format(principle) +", \n";
						display = display + "Your interest is: " + interest*100 + ", \n";
						display = display + "Your Loan term is: " + term + " years. \n\n\n";
						display = display + "Your monthly payment will be: "+ NumberFormat.getCurrencyInstance().format(payment);
						display = display + "Below is your Amortized Payment Schedule: \n\n\n";
						display = display + "Month "+ month +"\tPayment: "+ NumberFormat.getCurrencyInstance().format(payment) +
						"\tBalance: "+ NumberFormat.getCurrencyInstance().format(pbalance) +
						"\tPrincipal Paid: "+ NumberFormat.getCurrencyInstance().format(ppaid) +
						"\tInterest Paid: "+ NumberFormat.getCurrencyInstance().format(ipaid);

				displayArea.setText(display);
			}
			else {
				displayArea.setText("Please enter the correct information.");
			}
			}
		}
}

These are the errors that TextPad gave me when I tried to compile

C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:147: 'else' without 'if'
else if(ratestring == "15-year at 5.50%")
^
C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:153: 'else' without 'if'
else (display == "30-year at 5.755")
^
C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:166: 'catch' without 'try'
catch (Exception ex){
^
C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:137: 'try' without 'catch' or 'finally'
try {
^
4 errors

Tool completed with exit code 1

Recommended Answers

All 3 Replies

OK, I made few corrections, but this will not get your program compile as there is lot of things you need to correct

/**
*This JFrame is used to gather mortgage data from the user and display the monthly
*payment amount of the mortgage based on the input.
*
* @Title MortCalc2
* @author William Cook
* @Date 4/19/2009
* @version 1.1
*/

import java.util.*;
import java.text.NumberFormat;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MortCalc2r1
extends JFrame
implements ActionListener {

	//create labels and fields
	JLabel nameLabel = new JLabel("Hello. Please enter your name here.");
	JLabel principleLabel = new JLabel("Please enter your desired loan amount here.");
	JLabel ratestringLabel = new JLabel("Please select your rate and term.");
	JButton calcButton = new JButton("Calculate");
	JButton clearButton = new JButton("Clear Form");
	JButton exitButton = new JButton("Exit");
	JTextField nameTField = new JTextField ();
	JTextField principleTField = new JTextField ();
	JComboBox ratestring = newJComboBox(rateterm);
	
	//declare variables
	String [] rateterm={"7-year at 5.35%","15-year at 5.50%","30-year at 5.755"};
	String name = "unknown";
	String display = "0";
	double principle = 0;
	double payment;
	int month = 1;
	double pbalabce = 0;
	double ppaid = 0;
	double ipaid = 0;
	boolean fielderror = false;
	JTextArea displayArea = new JTextArea();
	
	private Container con = getContentPane();
	
	public static void main(String[] args) {
		MortCalc2r1 rta = new MortCalc2r1();
	}
	
	public MortCalc2r1() {
		try {
			jbInit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jbInit() throws Exception {
		setSize(600,600);
		setTitle("William Cook's Personal Mortgage Calculator");
		con.setLayout(null);
		
		nameLabel.setLocation(20,20);
		nameLabel.setSize(400,20);
		
		nameTField.setLocation(450,20);
		nameTField.setSize(80,20);
		
		principleLabel.setLocation(20,60);
		principleLabel.setSize(400,20);
		
		principleTField.setLocation(450,60);
		principleTField.setSize(80,20);
		
		ratestringLabel.setLocation(20,100);
		ratestringLabel.setSize(400,20);
		
		calcButton.setLocation(20,200);
		calcButton.setSize(100,20);
		
		clearButton.setLocation(150,200);
		clearButton.setSize(100,20);
		
		exitButton.setLocation(280,200);
		exitButton.setSize(100,20);
		
		con.add(nameLabel, null);
		con.add(nameTField, null);
		con.add(principleLabel, null);
		con.add(principleTField, null);
		con.add(ratestringLabel, null);
		con.add(ratestring, null);
		con.add(calcButton, null);
		con.add(clearButton, null);
		con.add(exitButton, null);
		con.add(displayArea, null);
		
		ratestring.addActionListener(this);
		calcButton.addActionListener(this);
		clearButton.addActionListener(this);
		exitButton.addActionListener(this);
		
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent thisEvent) {
	
		Object source = thisEvent.getSource();
		
		if (thisEvent.getSource() instanceof JComboBox) {
			JComboBox ratestring = (JComboBox) thisEvent.getSource();
		}
		
		if (source == calcButton) {
			displaydata();
		}
		
		if (source == clearButton) {
			nameTField.setText("");
			principleTField.setText("");
			interestTField.setText("");
			termTField.setText("");
			displayArea.setText("");
		}
		
		if(source == exitButton) {
			System.exit(0);
		}
	}
	public void displaydata() {
		
		name = nameTField.getText();
		try {
			principle = Double.parseDouble(principleTField.getText());
			fielderror = false; //missing semicolum
			pblance = principle;
			if (ratestring == "7-year at 5.35%")
			{
				double interest = 0.0535;
				int term = 7;
			}//; not need it
			
			else if(ratestring == "15-year at 5.50%")
			{
				double interest = 0.055;
				int term = 15;
			}//; not need it
			
			else if(display == "30-year at 5.755") //have to be else if once you provide condition
			{
				double interest = 0.0535;
				int term = 7;
			}//; not need it
			
			payment = ((interest/12) * principle)/(1-(Math.pow(1+(interest/12), (term * -12))));
			
			pbalance = pbalance - ppaid;
			ppaid = payment - ipaid;
			ipaid = ((interest/12)*pbalance);
			month++;
		}//add bracket to close try statement
			
		catch (Exception ex){
			fielderror = true;
		}
		if (!fielderror){
			while (pbalance > 0)
			String display = new String("Thank you for using the Personal Mortgage Calculator " + name + ", \n");
			display = display + "Your Principle balance is: "+ NumberFormat.getCurrencyInstance().format(principle) +", \n";
			display = display + "Your interest is: " + interest*100 + ", \n";
			display = display + "Your Loan term is: " + term + " years. \n\n\n";
			display = display + "Your monthly payment will be: "+ NumberFormat.getCurrencyInstance().format(payment);
			display = display + "Below is your Amortized Payment Schedule: \n\n\n";
			display = display + "Month "+ month +"\tPayment: "+ NumberFormat.getCurrencyInstance().format(payment) +
			"\tBalance: "+ NumberFormat.getCurrencyInstance().format(pbalance) +
				"\tPrincipal Paid: "+ NumberFormat.getCurrencyInstance().format(ppaid) +
				"\tInterest Paid: "+ NumberFormat.getCurrencyInstance().format(ipaid);
				
			displayArea.setText(display);
		}
		else {
		displayArea.setText("Please enter the correct information.");
		}// removed one of closing brackets
	}
}

Not sure what you trying to do in this section

if (!fielderror && ){
    while (pbalance > 0)

but as you run only one calculation at the time you can simplify it to if (!fielderror && pbalance > 0) . Once that done there are another 18 errors waiting for your correction

Thank you for noticing those things...after looking at what ou did I was able to find several more of my errors...many of them are spelling...I am still catching an error...I can not figure out what it is looking for...I also think my logic on this program might be flawed and that i need to tell each "if" statement to do the calculation and display to get it to actually loop through the amortization...not sure until I can get it to compile and see what it does...any suggestions?

C:\Documents and Settings\Shadow\Desktop\Java2 Class\MortCalc2r1.java:148: ')' expected
if (ratelist == 7-year at 5.35%)
^
1 error

Tool completed with exit code 1

/**
*This JFrame is used to gather mortgage data from the user and display the monthly
*payment amount of the mortgage based on the input.
*
* @Title	MortCalc2
* @author	William Cook
* @Date		4/19/2009
* @version	1.1
*/

//import necessary components
import java.util.*;
import java.text.NumberFormat;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MortCalc2r1
	extends JFrame
	implements ActionListener {

		//declare variables
		String [] ratestring={"7-year at 5.35%","15-year at 5.50%","30-year at 5.755"};
		String name = "unknown";
		String display = "0";
		double interest = 0;
		int term = 0;
		double principle = 0;
		double payment;
		int month = 1;
		double pbalance = 0;
		double ppaid = 0;
		double ipaid = 0;
		boolean fielderror = false;
		JTextArea displayArea = new JTextArea();

		//create labels and fields
		JLabel nameLabel = new JLabel("Hello. Please enter your name here.");
		JLabel principleLabel = new JLabel("Please enter your desired loan amount here.");
		JLabel ratelistLabel = new JLabel("Please select your rate and term.");
		JButton calcButton = new JButton("Calculate");
		JButton clearButton = new JButton("Clear Form");
		JButton exitButton = new JButton("Exit");
		JTextField nameTField = new JTextField ();
		JTextField principleTField = new JTextField ();
		JComboBox ratelist = new JComboBox(ratestring);

		//create container
		private Container con = getContentPane();

		public static void main(String[] args) {
			MortCalc2r1 rta = new MortCalc2r1();
		}

			public MortCalc2r1() {
				try {
					jbInit();
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}

			private void jbInit() throws Exception {

				//set location and size of all container items
				setSize(600,600);
				setTitle("William Cook's Personal Mortgage Calculator");
				con.setLayout(null);

				nameLabel.setLocation(20,20);
				nameLabel.setSize(400,20);

				nameTField.setLocation(450,20);
				nameTField.setSize(80,20);

				principleLabel.setLocation(20,60);
				principleLabel.setSize(400,20);

				principleTField.setLocation(450,60);
				principleTField.setSize(80,20);

				ratelistLabel.setLocation(20,100);
				ratelistLabel.setSize(400,20);

				calcButton.setLocation(20,200);
				calcButton.setSize(100,20);

				clearButton.setLocation(150,200);
				clearButton.setSize(100,20);

				exitButton.setLocation(280,200);
				exitButton.setSize(100,20);

				//add container items
				con.add(nameLabel, null);
				con.add(nameTField, null);
				con.add(principleLabel, null);
				con.add(principleTField, null);
				con.add(ratelistLabel, null);
				con.add(ratelist, null);
				con.add(calcButton, null);
				con.add(clearButton, null);
				con.add(exitButton, null);
				con.add(displayArea, null);

				//add listeners for combo box and buttons
				ratelist.addActionListener(this);
				calcButton.addActionListener(this);
				clearButton.addActionListener(this);
				exitButton.addActionListener(this);

				setVisible(true);
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}

			public void actionPerformed(ActionEvent thisEvent) {

				//set action for buttons and combo boxes
				Object source = thisEvent.getSource();

				if (thisEvent.getSource() instanceof JComboBox) {
					JComboBox ratelist = (JComboBox) thisEvent.getSource();
				}

				if (source == calcButton) {
					displaydata();
				}

				if (source == clearButton) {
					nameTField.setText("");
					principleTField.setText("");
					displayArea.setText("");
				}

				if(source == exitButton) {
					System.exit(0);
				}
			}
			public void displaydata() {

				name = nameTField.getText(); //get name string from user input
					try {
				principle = Double.parseDouble(principleTField.getText()); //parse the principle from user input
				fielderror = false;
				pbalance = principle; //sets new balance for each calculation so the program can cycle through payments to 0
				if (ratelist == 7-year at 5.35%) //trying to get it to recognize text from drop box and use that to determin what interest and term to use
						{
							double interest = 0.0535; //set interest based on combo Box
							int term = 7;//set term based on combo box
							}

				else if(ratelist == 15-year at 5.50%)
						{
							double interest = 0.055;//set interest based on combo box
							int term = 15;//set term based on combo box
							}

				else if(ratelist == 30-year at 5.755)
						{
							double interest = 0.0535;//set interest based on combo box
							int term = 7;//set term based on combo box
							}

							payment = ((interest/12) * principle)/(1-(Math.pow(1+(interest/12), (term * -12))));//calculation for mortgage payment

							pbalance = pbalance - ppaid;//find new balance for each month
							ppaid = payment - ipaid;//find total principal paid
							ipaid = ((interest/12)*pbalance);//find total interest paid
							month++;//add 1 to the month to move the While loop forward
				}
					catch (Exception ex){
						fielderror = true;
					}
					//display that should continue to loop until the balance reaches "0"
					if (!fielderror && pbalance > 0){
						String display = "Thank you for using the Personal Mortgage Calculator " + name + ", \n";
						display = display + "Your Principle balance is: "+ NumberFormat.getCurrencyInstance().format(principle) +", \n";
						display = display + "Your interest is: " + interest*100 + ", \n";
						display = display + "Your Loan term is: " + term + " years. \n\n\n";
						display = display + "Your monthly payment will be: "+ NumberFormat.getCurrencyInstance().format(payment);
						display = display + "Below is your Amortized Payment Schedule: \n\n\n";
						display = display + "Month "+ month +"\tPayment: "+ NumberFormat.getCurrencyInstance().format(payment) +
						"\tBalance: "+ NumberFormat.getCurrencyInstance().format(pbalance) +
						"\tPrincipal Paid: "+ NumberFormat.getCurrencyInstance().format(ppaid) +
						"\tInterest Paid: "+ NumberFormat.getCurrencyInstance().format(ipaid);

				displayArea.setText(display);
			}
			else {
				displayArea.setText("Please enter the correct information.");

			}
		}
}
  1. You never actually added JComboBox ratelist to your JFrame
    ratelistLabel.setLocation(20,100);
    ratelistLabel.setSize(400,20);
    				
    //adding ratelist
    ratelist.setLocation(450, 100);
    ratelist.setSize(120, 20);
    
    calcButton.setLocation(20,200);
    calcButton.setSize(100,20);
  2. To retrieve item from JComboBox you need to use getSelectedItem() method that will return Object type and this can be then made to String by passing it to toString() method that JComboBox inherits from Component class
  3. if/else statements needs to compare to Strings to find out if their equality. Use String.equals(String) to do so. Either directly to result taken from JComboBox ratelist.getSelectedItem().toString().equals("7-year at 5.35%") or assign return from JComboBox to new String and then compare
    String strRatelist = ratelist.getSelectedItem().toString();
    if(strRatelist.equals("7-year at 5.35%"))
    //rest of the code
  4. Your catch statement is still inside try. Take it out, including if/else statement after catch.
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.