Results 1 to 6 of 6
  1. #1
    felonee is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Mortgage Calculator w/GUI - won't display monthly payment on calculate

    I've done a search for Mortgage, as I know there are a LOT of posts for this topic. I wasn't able to determine what my problem is. I don't expect an "answer", please point me in the right direction though. I'm still not 100% comfortable with all of the terminology, so your patience is appreciated. I am using NetBeans. When I run my program, the box pops up, I type in variables and click Calculate. I get [] as the answer.

    Java Code:
    package mortgage.calculator;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;
    
    //Declaration of class
    public class MortgageCalculator extends JFrame implements ActionListener {
    
      
    //Create boxes and labels
    JPanel row1 = new JPanel( new GridLayout( 1, 3 ) );
    // Sets label text and alignment
    JLabel loanAmount_label1 = new JLabel( "Loan Amount:", JLabel.LEFT );
    JTextField loanAmount_txt = new JTextField( 10 );
    JPanel row2 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel loanTerm_label2 = new JLabel( "Loan Term:", JLabel.LEFT );
    JTextField loanTerm_txt = new JTextField( 10 );
    JPanel row3 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel interestRate_label3 = new JLabel( "Annual Interest Rate:", JLabel.LEFT );
    JTextField interestRate_txt = new JTextField( 10 );
    JPanel row4 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel monthlyPayment_label4 = new JLabel( "Monthly Payment:", JLabel.LEFT );
    JLabel payment=new JLabel();
    JPanel button = new JPanel( new FlowLayout( FlowLayout.CENTER ) );
    
    //Create buttons and define button labels
    JButton resetButton = new JButton( "Reset" );
    JButton exitButton = new JButton( "Exit" );
    JButton calcButton = new JButton( "Calculate" );
    
    //User input area
    JTextArea displayArea = new JTextArea( 10, 45 );
    
    //Declare variables
    double monthlyPayment;// Sets monthlyPayment as a double set to zero
    double monthlyInterest, denominator;
    double loanAmount;// Sets loanAmount as a double set to zero
    double interestRate;// Sets rate as a double set to zero
    int loanTerm;// Sets interest as a integer set to zero
           
    
    //Declaration of constructor
    public MortgageCalculator(){
    
    //Syntax used for calling constructor    
    super( "Mortgage Calculator: PRG421 Week 2" ); 
    //Set constructor size
    setSize( 400, 200 ); 
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    //returns content value
    Container pane = getContentPane(); 
    //provides label borders
    Border border = new EmptyBorder( 2, 5, 2, 5 ); 
    
    //Format user input windows
    pane.add( row1 );
    row1.add( loanAmount_label1 );
    row1.add( loanAmount_txt );
    row1.setMaximumSize( new Dimension( 300, 25 ));
    row1.setBorder( border );
    
    pane.add( row2 );
    row2.add( loanTerm_label2 );
    row2.add( loanTerm_txt );
    row2.setMaximumSize( new Dimension( 300, row2.getMinimumSize().height ));
    row2.setBorder( border );
    
    pane.add( row3 );
    row3.add( interestRate_label3 );
    row3.add( interestRate_txt );
    row3.setMaximumSize( new Dimension( 300, row3.getMinimumSize().height ));
    row3.setBorder( border );
    
    pane.add( row4 );
    row4.add( monthlyPayment_label4 );
    row4.add( payment );
    row4.setMaximumSize( new Dimension( 300, row4.getMinimumSize().height ));
    row4.setBorder( border );
    
    //Adds the buttons to the display
    button.add( calcButton );
    button.add( resetButton );
    button.add( exitButton );
    pane.add( button );
    
    pane.setLayout( new BoxLayout( pane, BoxLayout.Y_AXIS ));
    setVisible( true );
    setContentPane( pane );
    
    button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height ));
    
    //Adds action listener for buttons
    resetButton.addActionListener( this );
    exitButton.addActionListener( this );
    calcButton.addActionListener( this );
    }
    
    //Declares necessary calculations when "Calculate" is clicked
    
    public void actionPerformed( ActionEvent event ) {
    Object source = event.getSource();
    
    //Begin if function
    if( source == calcButton ) // calculates on click
    {
    //validates that there is input
    validateUserInput( loanAmount_txt, interestRate_txt, loanTerm_txt); 
    
    // formula for calculation output
    // Taken from http://www.hughchou.org/calc/formula.html
    // Formulat is M = P * (J / (1 - (1 + J)^-N))
    // where M = Mortgage payment, P = loanAmount, I = annual interest
    // L = length of loan, J = monthly interest, and N = number of months for loan
    
    monthlyInterest = interestRate/(12 * 100);
    denominator = Math.pow(1 + monthlyInterest, -360);
    denominator = 1 - denominator;
    monthlyPayment = loanAmount * (monthlyInterest / denominator);
    
    }
    
    //Format output numbers
    DecimalFormat df=new DecimalFormat( "$ ###,###.00" );
    
    payment.setText(df.format(monthlyPayment));
    
    //Clears all entries when "Reset" is clicked
    if( source == resetButton )
    {
    loanTerm_txt.setText( " " );
    interestRate_txt.setText( " " );
    loanAmount_txt.setText( " " );
    }
    
    //Closes the GUI when "Exit" is clicked
    if( source == exitButton )
    {System.exit( 0 );
    }
    }
    //End action
    
    // Declaration of the main method
    public static void main( String[] arguments ) {
    MortgageCalculator mtg = new MortgageCalculator();
    }
    
    // Declares user input for each variable
    public void validateUserInput( JTextField loanAmount_txt, JTextField rate_txt,
    JTextField term_txt ){
        
      
        
    {
     
    }    
    }    
    }

  2. #2
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Re: Mortgage Calculator w/GUI - won't display monthly payment on calculate

    Ok there are some things wrong with this:

    1. Initiate your variables (the doubles and the int) otherwise strange things might happen.... Always handy to initiate your variables before you start doing things with it..

    2. Maybe get the values out of your JTextfields and put them in the corresponding double? ValidateUserInput() is empty at the moment... (works fine for me if I change that)

    3. Is it rlly necessary to give the JTextfields as parameters to ValidateUserInput()??? They are in the same class -_-

  3. #3
    felonee is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Mortgage Calculator w/GUI - won't display monthly payment on calculate

    I tried to implement your suggestions. I thought that I did suggestion 2 correctly, but it's still not working for me.

    Java Code:
    package mortgage.calculator;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;
    
    //Declaration of class
    public class MortgageCalculator extends JFrame implements ActionListener {
    
    // Declaration of the main method
    public static void main( String[] arguments ) {
    MortgageCalculator mtg = new MortgageCalculator();
    }
    
    //Create boxes and labels
    JPanel row1 = new JPanel( new GridLayout() );
    // Sets label text and alignment
    JLabel loanAmount_label1 = new JLabel( "Loan Amount:" );
    JTextField loanAmount_txt = new JTextField();
    JPanel row2 = new JPanel( new GridLayout() );
    JLabel loanTerm_label2 = new JLabel( "Loan Term:" );
    JTextField loanTerm_txt = new JTextField();
    JPanel row3 = new JPanel( new GridLayout() );
    JLabel interestRate_label3 = new JLabel( "Annual Interest Rate:" );
    JTextField interestRate_txt = new JTextField();
    JPanel row4 = new JPanel( new GridLayout() );
    JLabel monthlyPayment_label4 = new JLabel( "Monthly Payment:");
    JLabel payment=new JLabel();
    JPanel button = new JPanel( new FlowLayout() );
    
    //Create buttons and define button labels
    JButton resetButton = new JButton( "Reset" );
    JButton exitButton = new JButton( "Exit" );
    JButton calcButton = new JButton( "Calculate" );
    
    //Declare variables
    double loanAmount = 0; // Sets loan amount as a integer set to zero
    int loanTerm = 0; // Sets loan length as a integer set to zero
    double interestRate = 0;// Sets interest as a double set to zero
    double monthlyInterest = interestRate/(12 * 100);
    double monthlyPayment = loanAmount * ( interestRate / ( 12 * 100 ) ) / ( 1 - Math.pow( 1 + ( interestRate / ( 12 * 100 ) ), -1 * ( loanTerm * 12 ) ) );
    
    //Declaration of constructor
    public MortgageCalculator(){
    
    //Syntax used for calling constructor    
    super( "Mortgage Calculator: PRG421 Week 2" ); 
    //Set constructor size
    setSize( 400, 200 ); 
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    //returns content value
    Container pane = getContentPane(); 
    //provides label borders
    Border border = new EmptyBorder( 2, 5, 2, 5 ); 
    
    //Format user input windows
    pane.add( row1 );
    row1.add( loanAmount_label1 );
    row1.add( loanAmount_txt );
    row1.setMaximumSize( new Dimension( 300, 25 ));
    row1.setBorder( border );
    
    pane.add( row2 );
    row2.add( loanTerm_label2 );
    row2.add( loanTerm_txt );
    row2.setMaximumSize( new Dimension( 300, row2.getMinimumSize().height ));
    row2.setBorder( border );
    
    pane.add( row3 );
    row3.add( interestRate_label3 );
    row3.add( interestRate_txt );
    row3.setMaximumSize( new Dimension( 300, row3.getMinimumSize().height ));
    row3.setBorder( border );
    
    pane.add( row4 );
    row4.add( monthlyPayment_label4 );
    row4.add( payment );
    row4.setMaximumSize( new Dimension( 300, row4.getMinimumSize().height ));
    row4.setBorder( border );
    
    //Adds the buttons to the display
    button.add( calcButton );
    button.add( resetButton );
    button.add( exitButton );
    pane.add( button );
    
    pane.setLayout( new BoxLayout( pane, BoxLayout.Y_AXIS ));
    setVisible( true );
    setContentPane( pane );
    
    button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height ));
    
    //Adds action listener for buttons
    resetButton.addActionListener( this );
    exitButton.addActionListener( this );
    calcButton.addActionListener( this );
    }
    
    //Declares necessary calculations when "Calculate" is clicked
    public void actionPerformed( ActionEvent event ) {
    Object source = event.getSource();
    
    //Format output numbers
    DecimalFormat df=new DecimalFormat( "$ ###,###.00" );
    
    payment.setText(df.format(monthlyPayment));
    
    //Clears all entries when "Reset" is clicked
    if( source == resetButton )
    {
    loanTerm_txt.setText( " " );
    interestRate_txt.setText( " " );
    loanAmount_txt.setText( " " );
    }
    
    //Closes the GUI when "Exit" is clicked
    if( source == exitButton )
    {System.exit( 0 );
    }
    }
    //End action
        
    {
     
    }    
    }    
    }

  4. #4
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Re: Mortgage Calculator w/GUI - won't display monthly payment on calculate

    mmh well you didn't implement them at all...
    probably because I'm making this way to difficult for you (srry bout that)

    Your previous example was almost perfect so throw away the one you have now and reload it!

    so:

    1. Initializing your doubles and int
    this means to do the following:

    Java Code:
    double loanAmount = 0; // Sets loan amount as a integer set to zero
    int loanTerm = 0; // Sets loan length as a integer set to zero
    double interestRate = 0;// Sets interest as a double set to zero
    double monthlyInterest = 0;
    double monthlyPayment =0;
    why would you do this? Because if you make a new variable (like a double) the computer reserves space for it, BUT you are not sure what is in this space.

    Try thinking of it like this: To keep your socks you reserve a drawer in your wardrobe. But are you certain there isn't other junk lying around in the drawer? Maybe checking if the drawer is empty would be a good idea? Because if you try to take out socks the next time you might be taking out the junk and not the socks! In java language this corresponds to initializing the variable to zero == removing all the junk & cleaning out the drawer.

    What you did in your latest example is assigning the value to the variabele and expect it to update when the other fields change...sadly Java doesn't work that way :(

    more info: here

    2. Getting the JTextfield values and putting them into the variables

    You should get back your ValidateUserInput and tell it that actually has to validate user input.
    Let me ask you something:
    You need the value of the user inputed Textfields right? And you need this value to be put into your doubles?
    Well..this is exactly what you need to do in ValidateUserInput()!! So get the value of the textfield and put it into the correct double!
    If you don't do this the variables will always be zero and you will get the [] result!


    Ignore point 3 for now

  5. #5
    felonee is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Mortgage Calculator w/GUI - won't display monthly payment on calculate

    I get "initializing the doubles and int" now. For some reason, I thought it was setting a value of 0, not setting a starting value. I understand this now and it makes sense. Thank you for the explanation and link. Can you give an example of #2? I feel like I'm pretty much right back where I started. I don't mean to seem dense, but I've been working on this program for over 8 hours and I'm kind of brain dead.

    Java Code:
    package mortgage.calculator;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;
    
    //Declaration of class
    public class MortgageCalculator extends JFrame implements ActionListener {
    
      
    //Create boxes and labels
    JPanel row1 = new JPanel( new GridLayout( 1, 3 ) );
    // Sets label text and alignment
    JLabel loanAmount_label1 = new JLabel( "Loan Amount:", JLabel.LEFT );
    JTextField loanAmount_txt = new JTextField( 10 );
    JPanel row2 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel loanTerm_label2 = new JLabel( "Loan Term:", JLabel.LEFT );
    JTextField loanTerm_txt = new JTextField( 10 );
    JPanel row3 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel interestRate_label3 = new JLabel( "Annual Interest Rate:", JLabel.LEFT );
    JTextField interestRate_txt = new JTextField( 10 );
    JPanel row4 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel monthlyPayment_label4 = new JLabel( "Monthly Payment:", JLabel.LEFT );
    JLabel payment=new JLabel();
    JPanel button = new JPanel( new FlowLayout( FlowLayout.CENTER ) );
    
    //Create buttons and define button labels
    JButton resetButton = new JButton( "Reset" );
    JButton exitButton = new JButton( "Exit" );
    JButton calcButton = new JButton( "Calculate" );
    
    //User input area
    JTextArea displayArea = new JTextArea( 10, 45 );
    
    // Declaration of number formats that will be output after calculation
    DecimalFormat twodigits = new DecimalFormat( "#,###.00" );
    
    //Declare variables
    double loanAmount = 0;// Sets loanAmount as a double set to zero
    int loanTerm = 0;// Sets interest as a integer set to zero
    double interestRate = 0;// Sets rate as a double set to zero
    double monthlyInterest = 0;// Sets montly Interest as a double set to zero
    double monthlyPayment = 0;// Sets monthlyPayment as a double set to zero
    
    //Declaration of constructor
    public MortgageCalculator(){
    
    //Syntax used for calling constructor    
    super( "Mortgage Calculator" ); 
    //Set constructor size
    setSize( 400, 200 ); 
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    //returns content value
    Container pane = getContentPane(); 
    //provides label borders
    Border border = new EmptyBorder( 2, 5, 2, 5 ); 
    
    //Format user input windows
    pane.add( row1 );
    row1.add( loanAmount_label1 );
    row1.add( loanAmount_txt );
    row1.setMaximumSize( new Dimension( 300, 25 ));
    row1.setBorder( border );
    
    pane.add( row2 );
    row2.add( loanTerm_label2 );
    row2.add( loanTerm_txt );
    row2.setMaximumSize( new Dimension( 300, row2.getMinimumSize().height ));
    row2.setBorder( border );
    
    pane.add( row3 );
    row3.add( interestRate_label3 );
    row3.add( interestRate_txt );
    row3.setMaximumSize( new Dimension( 300, row3.getMinimumSize().height ));
    row3.setBorder( border );
    
    pane.add( row4 );
    row4.add( monthlyPayment_label4 );
    row4.add( payment );
    row4.setMaximumSize( new Dimension( 300, row4.getMinimumSize().height ));
    row4.setBorder( border );
    
    //Adds the buttons to the display
    button.add( calcButton );
    button.add( resetButton );
    button.add( exitButton );
    pane.add( button );
    
    pane.setLayout( new BoxLayout( pane, BoxLayout.Y_AXIS ));
    setVisible( true );
    setContentPane( pane );
    
    button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height ));
    
    //Adds action listener for buttons
    resetButton.addActionListener( this );
    exitButton.addActionListener( this );
    calcButton.addActionListener( this );
    }
    
    //Declares necessary calculations when "Calculate" is clicked
    
    public void actionPerformed( ActionEvent event ) {
    Object source = event.getSource();
    // Begin "if" function
    if( source == calcButton ) // calculates on hit
    {
    validateUserInput( loanAmount_txt,  loanTerm_txt, interestRate_txt ); // validates input
    // formula for calculation output
    monthlyPayment = loanAmount * ( interestRate / ( 12 * 100 ) ) / ( 1 - Math.pow( 1 + ( interestRate / ( 12 * 100 ) ), -1 * ( loanTerm * 12 ) ) );
    payment.setText( "" + twodigits.format( monthlyPayment ) );
    }
    
    //Clears all entries when "Reset" is clicked
    if( source == resetButton )
    {
    loanTerm_txt.setText( " " );
    interestRate_txt.setText( " " );
    loanAmount_txt.setText( " " );
    }
    
    //Closes the GUI when "Exit" is clicked
    if( source == exitButton )
    {System.exit( 0 );
    }
    }
    //End action
    
    // Declaration of the main method
    public static void main( String[] arguments ) {
    MortgageCalculator mtg = new MortgageCalculator();
    }
    
    // Declares user input for each variable
    public void validateUserInput( JTextField loanAmount_txt, JTextField loanTerm_txt,
    JTextField interestRate_txt ){
    {
    }    
    }    
    }
    Last edited by felonee; 04-10-2012 at 12:44 AM.

  6. #6
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Re: Mortgage Calculator w/GUI - won't display monthly payment on calculate

    Ok, you need to ask yourself some simple questions:

    - What are the values of loanAmount,loanTerm,interestRate??
    - Do they ever change??
    - What value do I want them to be??
    - Where are the values I want??
    - How can I asign this value to the above variables??

    If you can answer these questions and make a simple modification to the procedure ValidateUserInput() you will get what you desire
    Last edited by Reskaillev; 04-10-2012 at 01:27 AM.

Similar Threads

  1. Mortgage Calculator
    By missmara77 in forum New To Java
    Replies: 1
    Last Post: 09-05-2011, 08:16 PM
  2. Mortgage Monthly Payment Formula
    By EpyonCustom in forum New To Java
    Replies: 5
    Last Post: 04-28-2011, 01:21 AM
  3. monthly mortgage payment formula
    By leoshiner in forum New To Java
    Replies: 2
    Last Post: 04-22-2011, 02:46 PM
  4. payment calculator
    By esallender in forum New To Java
    Replies: 4
    Last Post: 01-14-2011, 04:38 PM
  5. Payment Calculator help
    By rpetronejr in forum New To Java
    Replies: 3
    Last Post: 07-04-2010, 02:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •