I am over my head in my current class and could use help or pointers changing the program I have written. It is a mortgage calculator and I have to modify it. My class texts seem over my head and I have to drive 20 minutes and use public internet so tutorial online time is limited. My original program works but I am struggling to implement needed changes. My original program is:

import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class MortgageCalc extends JFrame {  //This creates all the buttons and windows
	
    JLabel firstNumLabel = new JLabel("Mortgage Principal:");
    JTextField firstNumField = new JTextField("0");
    JLabel secondNumLabel = new JLabel("Mortgage Terms (in years):");
    JTextField secondNumField = new JTextField("0");
    JLabel thirdNumLabel = new JLabel("Annual Percentage Rate (APR)");
    JTextField thirdNumField = new JTextField("0");
    JLabel resultLabel = new JLabel("Payment Amount:");
    JTextField resultField = new JTextField("0");
    JButton calcButton = new JButton("Calculate Payment");
    JButton closeButton = new JButton("Close");
    JButton resetButton = new JButton("Reset Calculator");
    public int pymtAmt = 0;
    public int intRate = 0;
    public int monMorTerms = 0;
    
    
    public MortgageCalc() {  //this creates the GUI
        super("Mortgage Calculator");
        setLocation(500, 200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        setLayout(new GridLayout(6, 2, 10, 10));
        
        add(firstNumLabel);
        add(firstNumField);
        add(secondNumLabel);
        add(secondNumField);
        add(thirdNumLabel);
        add(thirdNumField);
        add(resultLabel);
        add(resultField);
        add(calcButton);
        add(closeButton);
        add(resetButton);

        resultField.setEditable(false);

        closeButton.addActionListener(  //defines what the close button does
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        System.exit(0);
                    }
                });
        
        calcButton.addActionListener( //defines what the calculate button does
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        String firstNumText = firstNumField.getText();
                        String secondNumText = secondNumField.getText();
                        String thirdNumText = thirdNumField.getText();
                        double firstNum = Double.parseDouble(firstNumText);
                        double secondNum = Double.parseDouble(secondNumText);
                        double thirdNum = Double.parseDouble(thirdNumText);
                        double intRate = thirdNum / 100 / 12;
                        double monMorTerms = secondNum * 12;
                        double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
                        NumberFormat nf = NumberFormat.getCurrencyInstance();
                        resultField.setText("" + nf.format(result));
                    }
                });
        
        resetButton.addActionListener(new ActionListener() { //defines what the reset button does
        	public void actionPerformed(ActionEvent e) {
        		firstNumField.setText("0");
        		secondNumField.setText("0");
        		thirdNumField.setText("0");
        		resultField.setText("");
        	}
		});
        

        pack();
        setVisible(true);
    }

    public static void main(String[] args) { //creates a new instance of the MortgageCalc class
        MortgageCalc app = new MortgageCalc();
    }
}

I need to change it so that the user still inputs the loan amount, but the terms are listed in a drop down box. The instructions say to use an array for the mortgage data for the different loans and then display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection or quit. I have finally gotten it to create a box with the layout I like, but making it all function is what I am struggling with. Here is my new code for the changes so far:

import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class MortgageCalc extends JFrame {  //This creates all the buttons and windows
	
    JLabel firstNumLabel = new JLabel("Mortgage Principal:");
    JTextField firstNumField = new JTextField("0");
    JLabel resultLabel = new JLabel("Payment Amount:");
    JTextField resultField = new JTextField("0");
    JLabel comboOptions = new JLabel("Term Options");
    JButton calcButton = new JButton("Calculate Payment");
    JButton closeButton = new JButton("Close");
    JButton resetButton = new JButton("Reset Calculator");
    public int pymtAmt = 0;
    public int intRate = 0;
    public int monMorTerms = 0;
    public int secondNumField;
    public double thirdNumField;
    String[] loanTerms = { "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%" };
    JComboBox mortgageTerms = new JComboBox(loanTerms);
    
    
    
    
    
    public MortgageCalc() {  //this creates an instance of the MortgageCalc class
        super("Mortgage Calculator"); //this makes a title for the window
        setLocation(500, 200); //this stes the location of the window
        this.setDefaultCloseOperation(EXIT_ON_CLOSE); //this states the program will end when the window is closed

        setLayout(new GridLayout(6, 2, 10, 10)); //this defines the layout of the window
        
        add(firstNumLabel); //these lines add the elements to the window
        add(firstNumField);
        add(comboOptions);
        add(mortgageTerms);
        add(resultLabel);
        add(resultField);
        add(calcButton);
        add(closeButton);
        add(resetButton);

        resultField.setEditable(false); //this indicates the result field can not be edited by the user

        closeButton.addActionListener(  //defines what the close button does
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        System.exit(0);
                    }
                });
        
        calcButton.addActionListener( //defines what the calculate button does
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        String firstNumText = firstNumField.getText();
                        double firstNum = Double.parseDouble(firstNumText);
                        double intRate = 0;
                        double monMorTerms = 0;
                        double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
                        NumberFormat nf = NumberFormat.getCurrencyInstance();
                        resultField.setText("" + nf.format(result));
                    }
                });
        
        resetButton.addActionListener(new ActionListener() { //defines what the reset button does
        	public void actionPerformed(ActionEvent e) {
        		firstNumField.setText("0");
        		resultField.setText("");
        	}
		});
        

        pack();
        setVisible(true);
    }

    public static void main(String[] args) { //creates a new instance of the MortgageCalc class
        MortgageCalc app = new MortgageCalc();
    }
}

Any assistance or pointers, even if it is just to downloadable tutorials, would be appreciated. The assignment is most likely going to be late but I am not really concerned about that, the whole assignment is only worth 10 points. My main concerning is actually learning the material and so far the class texts are written above my comprehension level.

and start with this idea (don't forget that all about parsing double value from JComboBox is endless bullS***) from http://download.oracle.com/javase/tutorial/uiswing/index.html

import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.RoundingMode;
import javax.swing.*;

public class MortgageCalc extends JFrame {  //This creates all the buttons and windows

    private static final long serialVersionUID = 1L;
    private JLabel firstNumLabel = new JLabel("Mortgage Principal:");
    private JFormattedTextField firstNumField;
    private JLabel resultLabel = new JLabel("Payment Amount:");
    private JFormattedTextField resultField;
    private JLabel comboOptions = new JLabel("Term Options");
    private JButton calcButton = new JButton("Calculate Payment");
    private JButton closeButton = new JButton("Close");
    private JButton resetButton = new JButton("Reset Calculator");
    private int pymtAmt = 0;
    private int intRate = 0;
    private int monMorTerms = 0;
    private int secondNumField;
    private double thirdNumField;
    private String[] loanTerms = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
    private JComboBox mortgageTerms = new JComboBox(loanTerms);
    private NumberFormat formTextFieldFormat;
    private double nullAmount;

    public MortgageCalc() {  //this creates an instance of the MortgageCalc class
        super("Mortgage Calculator"); //this makes a title for the window
        //NumberFormat nf = NumberFormat.getCurrencyInstance();
        formTextFieldFormat = NumberFormat.getNumberInstance();// or let there getCurrencyInstance(), doesn't matter, with same results
        formTextFieldFormat.setMinimumFractionDigits(2);
        formTextFieldFormat.setMaximumFractionDigits(2);
        formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE); //this states the program will end when the window is closed
        setLayout(new GridLayout(6, 2, 10, 10)); //this defines the layout of the window
        add(firstNumLabel); //these lines add the elements to the window
        firstNumField = new JFormattedTextField(formTextFieldFormat);
        firstNumField.setValue(nullAmount);
        firstNumField.setHorizontalAlignment(SwingConstants.RIGHT);
        add(firstNumField);
        add(comboOptions);
        add(mortgageTerms);
        add(resultLabel);
        resultField = new JFormattedTextField(formTextFieldFormat);
        resultField.setValue(nullAmount);
        resultField.setHorizontalAlignment(SwingConstants.RIGHT);
        add(resultField);
        add(calcButton);
        add(closeButton);
        add(resetButton);
        resultField.setEditable(false); //this indicates the result field can not be edited by the user
        closeButton.addActionListener(new ActionListener() { //defines what the close button does

            @Override
            public void actionPerformed(ActionEvent evt) {
                System.exit(0);
            }
        });
        calcButton.addActionListener(new ActionListener() {//defines what the calculate button does

            @Override
            public void actionPerformed(ActionEvent evt) {
                String comboSelectedValue = (mortgageTerms.getSelectedItem().toString()).trim();
                int seachWordAt = comboSelectedValue.indexOf("at");
                String halfNumber = comboSelectedValue.substring(seachWordAt + 3, comboSelectedValue.length());
                String nearToNumber = halfNumber.trim();
                nearToNumber = nearToNumber.replace("%", "");
                String realNumber = nearToNumber.trim();
                double t1a0 = Double.parseDouble(realNumber);
                double t1a1 = (((Number) firstNumField.getValue()).doubleValue());
                double intRate = t1a0;
                double monMorTerms = 02;
                double result = 0.0;
                if (t1a1 != 0) {
                    result = (t1a1 * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
                } else {
                    JOptionPane.showMessageDialog(SwingUtilities.windowForComponent(calcButton),
                            "Please input Principal for Mortage ........ ", "Some Clear Message",
                            JOptionPane.ERROR_MESSAGE);
                    EventQueue.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            firstNumField.grabFocus();
                            firstNumField.requestFocus();
                            firstNumField.setText(firstNumField.getText());
                            firstNumField.selectAll();
                        }
                    });
                }
                resultField.setValue(result);
            }
        });
        resetButton.addActionListener(new ActionListener() { //defines what the reset button does

            @Override
            public void actionPerformed(ActionEvent e) {
                firstNumField.setValue(nullAmount);
                resultField.setValue(nullAmount);
                EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        mortgageTerms.setSelectedIndex(0);
                    }
                });
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(500, 200); //this stes the location of the window
        pack();
        setVisible(true);
    }

    public static void main(String[] args) { //creates a new instance of the MortgageCalc class
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                MortgageCalc app = new MortgageCalc();
            }
        });
    }
}

Use arrays for you loan information also. Then you can just loop through the information. for example
If he is wanting you use arrays it seems like he wants you to allow user be able to add more loans.
int[] Rate = new int[];

I have made some progress but I now have an error and I am not sure what I did wrong. Here is the updated code:

import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import static java.lang.Math.*;


public class MortgageCalc extends JFrame {  //This creates all the buttons and windows
    JLabel firstNumLabel = new JLabel("Mortgage Principal:");
    JTextField firstNumField = new JTextField("0");
    JLabel resultLabel = new JLabel("Payment Amount:");
    JTextField resultField = new JTextField("0");
    JLabel comboOptions = new JLabel("Term Options");
    JButton calcButton = new JButton("Calculate Payment");
    JButton closeButton = new JButton("Close");
    JButton resetButton = new JButton("Reset Calculator");
    public int pymtAmt = 0;
    public double intRate = 0;
    public int monMorTerms = 0;
    String[] loanTerms = { "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%" };
    JComboBox mortgageTerms = new JComboBox(loanTerms);
    
    
    
    
    
    public MortgageCalc() {  //this creates an instance of the MortgageCalc class
        super("Mortgage Calculator"); //this makes a title for the window
        setLocation(500, 200); //this stes the location of the window
        this.setDefaultCloseOperation(EXIT_ON_CLOSE); //this states the program will end when the window is closed

        setLayout(new GridLayout(6, 2, 10, 10)); //this defines the layout of the window
        
        add(firstNumLabel); //these lines add the elements to the window
        add(firstNumField);
        add(comboOptions);
        add(mortgageTerms);
        add(resultLabel);
        add(resultField);
        add(calcButton);
        add(closeButton);
        add(resetButton);

        resultField.setEditable(false); //this indicates the result field can not be edited by the user

        mortgageTerms.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        switch (mortgageTerms.getSelectedIndex()) {
                            case 0: monMorTerms = 84; intRate = 5.35 / 100 / 12;     break;
                            case 1: monMorTerms = 180; intRate = 5.5 / 100 / 12;    break;
                            case 2: monMorTerms = 360; intRate = 5.75 / 100 / 12; break;
                        }
                });
                
        
        
        closeButton.addActionListener(  //defines what the close button does
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        System.exit(0);
                    }
                });
        
        calcButton.addActionListener( //defines what the calculate button does
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        String firstNumText = firstNumField.getText();
                        double firstNum = Double.parseDouble(firstNumText);
                        double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
                        NumberFormat nf = NumberFormat.getCurrencyInstance();
                        resultField.setText("" + nf.format(result));
                    }
                });
        
        resetButton.addActionListener(new ActionListener() { //defines what the reset button does
        	public void actionPerformed(ActionEvent e) {
        		firstNumField.setText("0");
        		resultField.setText("");
       
                }
        	
	});
        

        pack();
        setVisible(true);
    
        
        public static void main(String[] args) { //creates a new instance of the MortgageCalc class
            MortgageCalc app = new MortgageCalc();
    }
}

The error is at the main method and it says "illegal start of expression".

Edited 5 Years Ago by Xrrak: n/a

This article has been dead for over six months. Start a new discussion instead.