Mortgage Calculator Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Mortgage Calculator Help

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Posts
    5

    Mortgage Calculator Help

    Hi I am taking a programming class and it requires me to write a java program to calculate mortgage with a GUI.
    Here is what I have so far. Where did I go wrong?

    Thanks in advance!
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Calculator extends JFrame implements ActionListener {
    private JPanel panelAdder;
    private JLabel labela;
    private JLabel labelt;
    private JLabel labelr;
    private JTextField textFieldAmount;
    private JTextField textFieldTerm;
    private JTextField textFieldRate;
    private JTextField textFieldResult;
    private JButton buttonCalc;
    
    public Calculator() {
      initComponents();
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      pack();
    
      // Add Listeners
      buttonCalc.addActionListener(this);
    }
    
    public void initComponents() {
     //Initialize Components
     panelAdder = new JPanel();
     labela = new JLabel("Amount");
     textFieldAmount = new JTextField();
     labelt = new JLabel("Term");
     textFieldTerm = new JTextField();
     labelr = new JLabel("Rate");
     textFieldRate = new JTextField();
     textFieldResult = new JTextField();
     buttonCalc = new JButton("Calculate");
    
     //Set Object Attributes
     textFieldResult.setEditable(false);
     textFieldResult.setColumns(8);
     textFieldAmount.setColumns(6);
     textFieldTerm.setColumns(2);
     textFieldRate.setColumns(2);
    
     Container contentPane = getContentPane();
     contentPane.setLayout(new FlowLayout());
    
     //Lets add the components to the panel
     panelAdder.add(labela);
     panelAdder.add(textFieldAmount);
     panelAdder.add(labelt);
     panelAdder.add(textFieldTerm);
     panelAdder.add(labelr);
     panelAdder.add(textFieldRate);
     panelAdder.add(buttonCalc);
     panelAdder.add(textFieldResult);
    
     contentPane.add(panelAdder);
    }
    
    public static void main(String[] args) {
      Calculator frame = new Calculator();
    }
    
    private void setResultValue() {
       double amount = Integer.parseInt(textFieldAmount.getText());
       double term = Integer.parseInt(textFieldTerm.getText());
       double rate = Integer.parseInt(textFieldRate.getText()) / 100.;
       double result = amount * ( rate * Math.pow ( ( 1 + rate ), term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 );
    
       textFieldResult.setText(Double.toString(result));
    }
    
    public void actionPerformed(ActionEvent event) {
      System.out.println("Action Button");
      String command = event.getActionCommand();
      if (command == "Calculate") {
          setResultValue();
      }
    }
    }
    Last edited by vwrado; 09-06-2005 at 01:57 PM.

  2. #2
    Join Date
    Aug 2004
    Posts
    46
    Where did I go wrong?
    I don't know. What is it doing that you don't expect? What are you expecting it to do that it doesn't? Any error messages when you run it? Any error messages when you compile?

    Also, when you post code, please post it between [code] and [/code] tags (you can use the "code" button on the message posting screen). It makes your code much easier to read.

    P.S. Re:
    Code:
     if (command == "Calculate") {
        setResultValue();
    }
    Don't confuse String's equals() method with the equality operator '=='. The == operator checks that two references refer to the same object. If you want to compare the contents of Strings (whether two strings contain the same character sequence), use equals(), e.g. if (str1.equals(str2))...

    Code:
    if ("Calculate".equals(command)) {
        setResultValue();
    }
    But more typically, if you just want to know if a particular button was pressed...
    Code:
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == buttonCalc) {
            setResultValue();
        }
    }
    Last edited by yawmark; 09-06-2005 at 01:35 PM.

  3. #3
    Join Date
    Aug 2005
    Posts
    5
    Thanks for the superfast response.
    It compiles without errors but when I run the program, input the numbers, it gives errors.

  4. #4
    Join Date
    Aug 2004
    Posts
    46
    when I run the program, input the numbers, it gives errors.
    Ahem. What errors? Please describe in detail what you're doing, what you expect to happen, and what you actually observe. If you have an error message, post the exact, complete error along with a full stack trace, if possible.

    P.S. If you're not entering the rate as an integer value (e.g., no decimal points), you're going to have problems...

  5. #5
    Join Date
    Aug 2005
    Posts
    5
    Im sorry, I'm just really frustrated that I have been working on this for hours and I dont even know what I am typing.

    anyways...

    after running the program and enterning in amount, term, and rate, I am getting

    java.lang.NumberFormatException: For input string: "5.35"

  6. #6
    Join Date
    Aug 2004
    Posts
    46
    java.lang.NumberFormatException: For input string: "5.35"
    Since you're using Integer.parseInt() to parse this string value, it should be pretty clear what's going wrong. You're trying to parse an int value from a string representation of a floating-point value.

    Have a look at the API for a class that can parse double values...

  7. #7
    Join Date
    Aug 2005
    Posts
    5
    Quote Originally Posted by yawmark
    P.S. If you're not entering the rate as an integer value (e.g., no decimal points), you're going to have problems...
    You have found my problem before I even posted.

Similar Threads

  1. Mortgage Calculator Help Requested
    By Richbezza in forum C++
    Replies: 3
    Last Post: 05-09-2007, 03:16 AM
  2. Mortgage Calculator
    By phamber in forum ASP.NET
    Replies: 1
    Last Post: 07-22-2005, 09:55 AM
  3. PowerBuilder project - Mortgage Industry
    By kremburlee in forum Careers
    Replies: 0
    Last Post: 06-29-2005, 04:36 PM
  4. Mortgage Calc - URGENT Help
    By chadonline in forum Java
    Replies: 1
    Last Post: 06-06-2005, 07:39 AM
  5. Mortgage Calcutator
    By Eric Dillard in forum Java
    Replies: 0
    Last Post: 06-06-2002, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center