?
Solved

Java gui Mortgage calc

Posted on 2011-04-29
3
Medium Priority
?
258 Views
Last Modified: 2012-05-11
ok, I only have one error, which at one point wasn't there, so I'm not sure what I changed to make it error out on me. The error is "illegal start of expression" at the "public void onCalcButtonClick(ActionEvent e){" area.

What am I missing?
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Main implements Runnable{

	//The main container for this application
	public JFrame jfrMain;
        public JTextField fnameTextField;
        public JTextField lnameTextField;
        public JTextField phoneTextField;
	public JTextField loanAmountField;
	public JTextField termYearsField;
	public JTextField interestRateField;
	public JTextField monthlyPaymentField;
	public JButton calcButton;
        public JButton nextButton;
        public JButton exitButton;

	/**
	 * 
	 * @param args Command line arguments (not used)
	 */
	public static void main(String[] args) {
		try {
			SwingUtilities.invokeAndWait(new Main());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Setup the GUI and start the program.
	 */
	public void run() {
		jfrMain = new JFrame("Mortgage Calculator");
		jfrMain.setLayout(new GridLayout(5, 2));
		jfrMain.setSize(new Dimension(500,700));

		//Construct the fields
                fnameTextField = new JTextField(20);
                lnameTextField = new JTextField (20);
                phoneTextField = new JTextField (20);
		loanAmountField = new JTextField(10);
		termYearsField = new JTextField(4);
		interestRateField = new JTextField(2);
		monthlyPaymentField = new JTextField(10);
		monthlyPaymentField.setEditable(false);

		//adding the labels
                jfrMain.add(new JLabel("First Name"));
                jfrMain.add(fnameTextField);
                jfrMain.add(new JLabel("Last Name"));
                jfrMain.add(lnameTextField);
                jfrMain.add(new JLabel ("Phone"));
                jfrMain.add (phoneTextField);
		jfrMain.add(new JLabel("Loan Amount"));
		jfrMain.add(loanAmountField);
		jfrMain.add(new JLabel(" Loan Terms in months"));
		jfrMain.add(termYearsField);
		jfrMain.add(new JLabel("Interest Rate"));
		jfrMain.add(interestRateField);
		jfrMain.add(new JLabel("Monthly payment"));
		jfrMain.add(monthlyPaymentField);

		
                //calculate button
		calcButton = new JButton("Calculate");
		calcButton.addActionListener(new ActionListener()
                {
			public void actionPerformed(ActionEvent e)
                        {
				onCalcButtonClick(e);
			}
                                                                  
		})               
                        ;
                jfrMain.add(calcButton);
		jfrMain.setVisible(true);

	       // next button (still workiing on it)
             nextButton = new JButton("Next");
             nextButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
             nextButton.setActionCommand("Next");     
                }
	   });
            jfrMain.add(nextButton);
	    jfrMain.setVisible(true);
   
            //exit button still working on
            //exitButton = new JButton ("Exit");
            //exitButton.addActionListener (new ActionListener() {
            //public void actionPerformed(ActionEvent e)
            //    {
             //     System.exit(0);
             //       }
             //      });
           // jfrMain.add(exitButton);
	   // jfrMain.setVisible(true);
              


	public void onCalcButtonClick(ActionEvent e) {
		
             
		double loanAmount;
		double termYears;
		double interestRate;

		try{
			loanAmount = Double.parseDouble(loanAmountField.getText());
		} catch (NumberFormatException nf){
			JOptionPane.showMessageDialog(jfrMain, "Please enter loan amount");
			return;
		}

		try{
			termYears = Double.parseDouble(termYearsField.getText());
		} catch (NumberFormatException nf){
			JOptionPane.showMessageDialog(jfrMain, "Please enter mortgage term length");
			return;
		}

		try{
			interestRate = Double.parseDouble(interestRateField.getText()) / 100;
		} catch (NumberFormatException nf){
			JOptionPane.showMessageDialog(jfrMain, "Please enter interest rate");
			return;
		}

		//Calculate monthly payment 
		double payment = calculatePayment(loanAmount, termYears, interestRate);
		monthlyPaymentField.setText(new Double(payment).toString());

	}

	public double calculatePayment(double loanAmount, double termYears, double interestRate) {
		double monthlyInterest = interestRate / 12;
		double monthlyPayment = (double) (loanAmount * (monthlyInterest / (1 - Math.pow(1 + monthlyInterest, termYears * -1))));
		monthlyPayment = Math.ceil(monthlyPayment * 100) / 100;
		return monthlyPayment;
	}

}

Open in new window

0
Comment
Question by:marchbaby
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 35491294
Looks like a missing brace
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class Main implements Runnable {
    //The main container for this application
    public JFrame jfrMain;
    public JTextField fnameTextField;
    public JTextField lnameTextField;
    public JTextField phoneTextField;
    public JTextField loanAmountField;
    public JTextField termYearsField;
    public JTextField interestRateField;
    public JTextField monthlyPaymentField;
    public JButton calcButton;
    public JButton nextButton;
    public JButton exitButton;

    /**
     *
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Main());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * Setup the GUI and start the program.
     */
    public void run() {
        jfrMain = new JFrame("Mortgage Calculator");
        jfrMain.setLayout(new GridLayout(5, 2));
        jfrMain.setSize(new Dimension(500, 700));

        //Construct the fields
        fnameTextField = new JTextField(20);
        lnameTextField = new JTextField(20);
        phoneTextField = new JTextField(20);
        loanAmountField = new JTextField(10);
        termYearsField = new JTextField(4);
        interestRateField = new JTextField(2);
        monthlyPaymentField = new JTextField(10);
        monthlyPaymentField.setEditable(false);

        //adding the labels
        jfrMain.add(new JLabel("First Name"));
        jfrMain.add(fnameTextField);
        jfrMain.add(new JLabel("Last Name"));
        jfrMain.add(lnameTextField);
        jfrMain.add(new JLabel("Phone"));
        jfrMain.add(phoneTextField);
        jfrMain.add(new JLabel("Loan Amount"));
        jfrMain.add(loanAmountField);
        jfrMain.add(new JLabel(" Loan Terms in months"));
        jfrMain.add(termYearsField);
        jfrMain.add(new JLabel("Interest Rate"));
        jfrMain.add(interestRateField);
        jfrMain.add(new JLabel("Monthly payment"));
        jfrMain.add(monthlyPaymentField);

        //calculate button
        calcButton = new JButton("Calculate");
        calcButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onCalcButtonClick(e);
                }
            });

        jfrMain.add(calcButton);
        jfrMain.setVisible(true);

        // next button (still workiing on it)
        nextButton = new JButton("Next");
        nextButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    nextButton.setActionCommand("Next");
                }
            });
        jfrMain.add(nextButton);
        jfrMain.setVisible(true);

        //exit button still working on
        //exitButton = new JButton ("Exit");
        //exitButton.addActionListener (new ActionListener() {
        //public void actionPerformed(ActionEvent e)
        //    {
        //     System.exit(0);
        //       }
        //      });
        // jfrMain.add(exitButton);
        // jfrMain.setVisible(true);
    }

    public void onCalcButtonClick(ActionEvent e) {
        double loanAmount;
        double termYears;
        double interestRate;

        try {
            loanAmount = Double.parseDouble(loanAmountField.getText());
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain, "Please enter loan amount");

            return;
        }

        try {
            termYears = Double.parseDouble(termYearsField.getText());
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain,
                "Please enter mortgage term length");

            return;
        }

        try {
            interestRate = Double.parseDouble(interestRateField.getText()) / 100;
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain, "Please enter interest rate");

            return;
        }

        //Calculate monthly payment 
        double payment = calculatePayment(loanAmount, termYears, interestRate);
        monthlyPaymentField.setText(new Double(payment).toString());
    }

    public double calculatePayment(double loanAmount, double termYears,
        double interestRate) {
        double monthlyInterest = interestRate / 12;
        double monthlyPayment = (double) (loanAmount * (monthlyInterest / (1 -
            Math.pow(1 + monthlyInterest, termYears * -1))));
        monthlyPayment = Math.ceil(monthlyPayment * 100) / 100;

        return monthlyPayment;
    }
}

Open in new window

0
 

Author Closing Comment

by:marchbaby
ID: 35491634
LMAO It figures I would miss something so easy.... Thank you!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35491649
:)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month5 days, 15 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question