Hello, i need assistance with a mortgage calculator program.

the requirements are :
Program designed with GUI to Write the program in Java (with a graphical user interface)
and have it calculate then display the mortgage payment amount from user input of the amount
of the mortgage and the user's selection from a menu of available mortgage loans:

  • 7 years at 5.35%
  • 15 years at 5.5%
  • 30 years at 5.75%

Use an array for the mortgage data for the different loans.
Display the mortgage payment amount followed by the loan balance and
interest paid for each payment over the term of the loan.

Here is what i have. It compiles and runs, but then when you click on the drop down menu, for the rate/term, it doesn't coordinate with the proper fields. EXCEPTION errors come up. Help would be appreciated.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.text.DecimalFormat; 


 class mortgageGUI3 extends JFrame implements ActionListener     // frame class and inheritance 
    {
       static DecimalFormat decmat = new DecimalFormat("$#,##0.00");
        FlowLayout flow = new FlowLayout();
        JLabel amount1 = new JLabel("Amount: ");
        JTextField amount = new JTextField(10);
        JLabel rate1 = new JLabel("Rate: ");
        JTextField rate = new JTextField(4);
        JLabel term1 = new JLabel("Term: ");
        JTextField term = new JTextField(4);
        JComboBox mortgageBox = new JComboBox();
        JLabel mortgageList = new JLabel("Choose the Mortgage Loans");
        JButton calcButton = new JButton("Calculate Total");
        JButton exitButton = new JButton("Exit");
        JButton reset = new JButton("Reset");
        JLabel blankSpaces1 = new JLabel("                 ");
        JLabel blankSpaces2 = new JLabel("                 ");
        JLabel result = new JLabel("The total is:");
        JLabel sum = new JLabel("");

    public mortgageGUI3()
    {
        Container con = getContentPane();

            con.setLayout(flow);   // places components in a row
            con.add(amount1);
            con.add(amount);
            con.add(rate1);
            con.add(rate);
            con.add(term1);
            con.add(term);
            con.add(calcButton);
            con.add(blankSpaces1);
            con.add(result);
            con.add(sum);
            mortgageBox.addItem("The Mortgage Inputs Above");
            mortgageBox.addItem(" 7-year Mortgage at 5.35%");   //terms
            mortgageBox.addItem("15-year Mortgage at 5.5%");
            mortgageBox.addItem("30-year Mortgage at 5.75%");
            con.add(blankSpaces2);
            con.add(exitButton);
            con.add(reset);
            con.add(mortgageList);
            con.add(mortgageBox);


        amount.setBackground(Color.yellow);       // setting the background to make it attractive
        rate.setBackground(Color.yellow);
        term.setBackground(Color.yellow);
        calcButton.setForeground(Color.black);
        calcButton.setBackground(Color.yellow);

        exitButton.setForeground(Color.black);
        exitButton.setBackground(Color.yellow);

        reset.setForeground(Color.black);
        reset.setBackground(Color.yellow);

        mortgageBox.addActionListener(this);
        mortgageBox.setForeground(Color.white);
        mortgageBox.setBackground(Color.black);

        con.setBackground(Color.white);         //sets the background color

    calcButton.addActionListener(this);
    exitButton.addActionListener(this);
    reset.addActionListener(this);
    }
    public static void main(String[] args)      // main program
        {
            mortgageGUI3 cFrame = new mortgageGUI3();
            cFrame.setTitle("McBride's Mortgage Calculator");
            cFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            cFrame.setSize(300,300);
            cFrame.setVisible(true);
             }
            public void actionPerformed(ActionEvent e)
            {
                Object source = e.getSource();
                    Object resetButton = null;
                    if(source == calcButton)
                        {
        try                                                        // exception handling
            {
                String amt = amount.getText();
                String trm = term.getText();
                String rte = rate.getText();
                double a1 = Double.parseDouble(amt);
                double r1 = Double.parseDouble(rte);
                if(r1 == 0) getCleanScreen();
                double t1 = Double.parseDouble(trm);
                if(t1 == 0) getCleanScreen();

            java.text.DecimalFormat dec = new java.text.DecimalFormat(",##0.00");
            java.text.DecimalFormat doldec = new java.text.DecimalFormat("$,##0.00");

                    double mortgagefigure = getPayment(a1, r1, t1, 12);
                    double total = getFigure(mortgagefigure);
                    String output = "" + doldec.format(total);

    sum.setText("<font face='Arial' size='7' color='blue'>"+output+"</font>");
                }
                catch (NumberFormatException error)
                {
                    getCleanScreen();
                }
            }
            else if (source == mortgageBox)
            {
            int sizeNum = mortgageBox.getSelectedIndex();
                if (sizeNum == 0)
                    try
                    {
                        getCleanScreen();
                        }
                    catch (NumberFormatException error)
                    {
                        getCleanScreen();
                        }
                    else if (sizeNum == 1)
                    {
                       try
                           {
                            String amt = amount.getText();
                            double a1 = Double.parseDouble(amt);
                            double r1 = 7;
                            double t1 = 5.35;

            java.text.DecimalFormat dec = new java.text.DecimalFormat(",##0.00");
            java.text.DecimalFormat doldec = new java.text.DecimalFormat("$,##0.00");

                    double mortgagefigure = getPayment(a1, r1, t1, 12);
                    double total = getFigure(mortgagefigure);
                    String output = "" + doldec.format(total);

                        rate.setText("");
                        term.setText("");
                        sum.setText("<font face='Arial' size='7' color='blue'>"+output+"</font>");
                        }
                        catch (NumberFormatException error)
                        {
                            getCleanScreen();
                        }
                    }
                    else if (sizeNum == 2)
                    {
                        try
                        {
                        String amt = amount.getText();
                        double a1 = Double.parseDouble(amt);
                        double r1 = 15;
                        double t1 = 5.5;

        java.text.DecimalFormat dec = new java.text.DecimalFormat(",##0.00");
        java.text.DecimalFormat doldec = new java.text.DecimalFormat("$,##0.00");

                double mortgagefigure = getPayment(a1, r1, t1, 12);
                double total = getFigure(mortgagefigure);
                String output = "" + doldec.format(total);

                rate.setText("");
                term.setText("");
sum.setText("<font face='Arial' size='7' color='blue'>"+output+"</font>");
    }
        catch (NumberFormatException error)
            {
                getCleanScreen();
        }
        }
            else
                {
                    try
                        {
            String amt = amount.getText();
            double a1 = Double.parseDouble(amt);
            double r1 = 30;
            double t1 = 5.75;

        java.text.DecimalFormat dec = new java.text.DecimalFormat(",##0.00");
        java.text.DecimalFormat doldec = new java.text.DecimalFormat("$,##0.00");

        double mortgagefigure = getPayment(a1, r1, t1, 12);
        double total = getFigure(mortgagefigure);
                    String output = "" + doldec.format(total);

        rate.setText("");
        term.setText("");
        sum.setText("<font face='Arial' size='7' color='blue'>"+output+"</font>");
            }
                catch (NumberFormatException error)
                    {
                    getCleanScreen();
                        }
                    }
                }
                else if(source == resetButton)
                {
                    getCleanScreen();
                }
                else
                {

                System.exit(0);
            }
        }// end actionPerformed
        private void getCleanScreen() {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private double getFigure(double mortgagefigure) {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private double getPayment(double a1, double r1, double t1, int i) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
}

Dani AI

Generated

— the exceptions you're seeing happen for three clear reasons in the code you posted: (1) the methods getPayment, getFigure and getCleanScreen are still throwing UnsupportedOperationException so any action that calls them will crash; (2) in the combo-box branches you swapped rate and term (you assign r1 = 7 and t1 = 5.35 instead of rate = 5.35, term = 7); and (3) there is a local Object resetButton = null; inside actionPerformed that hides the real reset button reference, so the reset check never works. was right — read the stack trace: it will point you to the UnsupportedOperationException line first.

Fixes and minimal implementations to drop in:

  • Implement getPayment with the standard amortization formula (convert percent -> monthly decimal, handle zero-rate).
  • Implement getFigure to return total paid = monthlyPayment * months.
  • Implement getCleanScreen to clear fields/labels (or reset to defaults).
  • Remove the local resetButton variable and compare source == reset.

Example implementations (fit them into your class and adjust names/signatures as needed):

private double getPayment(double principal, double annualPct, int years) {
    double r = annualPct / 100.0 / 12.0;
    int n = years * 12;
    if (r == 0) return principal / n;
    double f = Math.pow(1 + r, n);
    return principal * (r * f) / (f - 1);
}

private double getFigure(double monthlyPayment, int years) {
    return monthlyPayment * years * 12;
}

private void getCleanScreen() {
    amount.setText("");
    rate.setText("");
    term.setText("");
    sum.setText("");
}

Use arrays for the mortgage data instead of duplicating branches:

double[] terms = {7, 15, 30};
double[] rates = {5.35, 5.50, 5.75};
int idx = mortgageBox.getSelectedIndex() - 1; // account for the prompt item
if (idx >= 0) {
    double monthly = getPayment(principal, rates[idx], (int)terms[idx]);
    double total = getFigure(monthly, (int)terms[idx]);
    sum.setForeground(Color.BLUE);
    sum.setText(new java.text.DecimalFormat("$#,##0.00").format(monthly));
}

Other tips: don't store HTML as escaped text — either set a JLabel with actual HTML (start with "<html>") or use setForeground/setFont. Show user-friendly error dialogs (JOptionPane) on NumberFormatException. Finally, split UI and calculation logic into separate methods/classes so the GUI code stays simple and easy to test.

use code tags. Don't try to squeeze everything into one big class. Read error messages carefully, they tell you what's wrong.

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.