Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please Help!!!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please Help!!!

    I am having trouble with my code. It is an ammortization calculator and it works fine except that the first time I click on the calculate button I get these funky little boxes instead of my interest, payment, and balance numbers. If I click it again, it works fine. Please help! My code is:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author Warbird
    */
    import javax.swing.*;
    import java.awt.*;
    import java.text.*;
    import java.awt.event.*;
    import java.lang.String.*;
    import java.text.NumberFormat;
    import java.text.DecimalFormat;
    public class WeekThreeCalc extends javax.swing.JFrame {


    double amount = 0;
    double[] iRate = {.0535, .055, .0575};
    double iRate1 = iRate [2];
    double[] years = {7, 15, 30};
    double interest = 0.0;
    double term = years [2];
    double mpay;
    double rate;
    double tMonths = 0.0;
    double mPayment1;
    double balance = 0;
    double mRatePaid = 0.00;
    double mPrinciple = 0.00;
    int months = 0;
    double mRate = 0;
    DecimalFormat ePay = new DecimalFormat ("$###,##0.00");;
    NumberFormat currency =
    NumberFormat.getCurrencyInstance();

    public WeekThreeCalc() {
    initComponents();
    }

    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    lAmountLabel = new javax.swing.JLabel();
    termIntLabel = new javax.swing.JLabel();
    mPaymentLabel = new javax.swing.JLabel();
    termInt = new javax.swing.JComboBox();
    lAmount = new javax.swing.JTextField();
    mPayment = new javax.swing.JTextField();
    results = new java.awt.ScrollPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    exitButton = new javax.swing.JButton();
    calcButton = new javax.swing.JButton();
    clearButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    lAmountLabel.setText("Loan Amount");

    termIntLabel.setText("Term/Interest");

    mPaymentLabel.setText("Monthly Payment");

    termInt.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Loan Term and Interest", "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%" }));

    mPayment.setEditable(false);

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    results.add(jScrollPane1);

    exitButton.setText("Exit");
    exitButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    exitButtonActionPerformed(evt);
    }
    });

    calcButton.setText("Calculate!");
    calcButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    calcButtonActionPerformed(evt);
    }
    });

    clearButton.setText("Clear");
    clearButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    clearButtonActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(22, 22, 22)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(lAmountLabel)
    .addComponent(lAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(69, 69, 69)
    .addComponent(termIntLabel))
    .addGroup(layout.createSequentialGroup()
    .addGap(18, 18, 18)
    .addComponent(termInt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addGap(40, 40, 40)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(mPayment, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(mPaymentLabel)))
    .addGroup(layout.createSequentialGroup()
    .addGap(48, 48, 48)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
    .addGroup(layout.createSequentialGroup()
    .addComponent(exitButton)
    .addGap(54, 54, 54)
    .addComponent(calcButton)
    .addGap(87, 87, 87)
    .addComponent(clearButton))
    .addComponent(results, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(24, 24, 24)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(lAmountLabel)
    .addComponent(termIntLabel)
    .addComponent(mPaymentLabel))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(termInt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(lAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(mPayment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(124, 124, 124)
    .addComponent(results, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 165, Short.MAX_VALUE)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(clearButton)
    .addComponent(calcButton)
    .addComponent(exitButton))
    .addContainerGap())
    );

    pack();
    }// </editor-fold>

    private void exitButtonActionPerformed(java.awt.event.ActionEve nt evt) {
    System.exit(0);
    }

    private void clearButtonActionPerformed(java.awt.event.ActionEv ent evt) {
    lAmount.setText("");
    mPayment.setText("");
    jTextArea1.setText("");
    }

    private void calcButtonActionPerformed(java.awt.event.ActionEve nt evt) {
    Integer termInt1;
    termInt1 = termInt.getSelectedIndex();
    if (termInt1 == 0)
    {

    }
    if (termInt1 == 1)
    {
    interest = iRate [0];
    term = years [0];
    }
    if (termInt1 == 2)
    {
    interest = iRate [1];
    term = years [1];
    }
    if (termInt1 == 3)
    {
    interest = iRate [2];
    term = years [2];
    }
    double monthRate = iRate1 /12;

    mPayment1 = amount *(monthRate / (1-Math.pow(1 + monthRate, -tMonths)));
    amount = Double.parseDouble(lAmount.getText());
    mPayment.setText(currency.format(mPayment1) + " ");
    repaint();
    tMonths = term * 12;
    balance = amount;


    for (months = 1; months <= tMonths; months++)
    {
    mRate = iRate1 /12;
    mRatePaid = balance * mRate;
    mPrinciple = mPayment1 - mRatePaid;
    balance = balance - mPrinciple;
    jTextArea1.append(" "+ months + "\t\t " + ePay.format(mPayment1) +"\t\t" + ePay.format(mRatePaid) + "\t\t" + ePay.format (balance) + "\n");
    }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    /*
    * Set the Nimbus look and feel
    */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
    * If Nimbus (introduced in Java SE 6) is not available, stay with the
    * default look and feel. For details see
    * How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(WeekThreeCalc.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(WeekThreeCalc.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(WeekThreeCalc.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(WeekThreeCalc.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
    }
    //</editor-fold>

    /*
    * Create and display the form
    */
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new WeekThreeCalc().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton calcButton;
    private javax.swing.JButton clearButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField lAmount;
    private javax.swing.JLabel lAmountLabel;
    private javax.swing.JTextField mPayment;
    private javax.swing.JLabel mPaymentLabel;
    private java.awt.ScrollPane results;
    private javax.swing.JComboBox termInt;
    private javax.swing.JLabel termIntLabel;
    // End of variables declaration
    }

  2. #2
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help!!!

    Sorry about that. I did not read the formatting rules on the page. Hope this helps.

     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Warbird
     */
     import javax.swing.*;
     import java.awt.*;
     import java.text.*;
     import java.awt.event.*;
     import java.lang.String.*;
     import java.text.NumberFormat;
     import java.text.DecimalFormat;
     public class WeekThreeCalc extends javax.swing.JFrame {
     
     
    double amount = 0;
     double[] iRate = {.0535, .055, .0575};
     double iRate1 = iRate [2];
     double[] years = {7, 15, 30};
     double interest = 0.0;
     double term = years [2];
     double mpay;
     double rate;
     double tMonths = 0.0;
     double mPayment1;
     double balance = 0;
     double mRatePaid = 0.00;
     double mPrinciple = 0.00;
     int months = 0;
     double mRate = 0;
     DecimalFormat ePay = new DecimalFormat ("$###,##0.00");;
     NumberFormat currency = 
    NumberFormat.getCurrencyInstance();
     
     public WeekThreeCalc() {
     initComponents();
     }
     
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
     @SuppressWarnings("unchecked")
     // <editor-fold defaultstate="collapsed" desc="Generated Code">
     private void initComponents() {
     
    lAmountLabel = new javax.swing.JLabel();
     termIntLabel = new javax.swing.JLabel();
     mPaymentLabel = new javax.swing.JLabel();
     termInt = new javax.swing.JComboBox();
     lAmount = new javax.swing.JTextField();
     mPayment = new javax.swing.JTextField();
     results = new java.awt.ScrollPane();
     jScrollPane1 = new javax.swing.JScrollPane();
     jTextArea1 = new javax.swing.JTextArea();
     exitButton = new javax.swing.JButton();
     calcButton = new javax.swing.JButton();
     clearButton = new javax.swing.JButton();
     
    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
     
    lAmountLabel.setText("Loan Amount");
     
    termIntLabel.setText("Term/Interest");
     
    mPaymentLabel.setText("Monthly Payment");
     
    termInt.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Loan Term and Interest", "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%" }));
     
    mPayment.setEditable(false);
     
    jTextArea1.setColumns(20);
     jTextArea1.setRows(5);
     jScrollPane1.setViewportView(jTextArea1);
     
    results.add(jScrollPane1);
     
    exitButton.setText("Exit");
     exitButton.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent evt) {
     exitButtonActionPerformed(evt);
     }
     });
     
    calcButton.setText("Calculate!");
     calcButton.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent evt) {
     calcButtonActionPerformed(evt);
     }
     });
     
    clearButton.setText("Clear");
     clearButton.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent evt) {
     clearButtonActionPerformed(evt);
     }
     });
     
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
     getContentPane().setLayout(layout);
     layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
     .addGroup(layout.createSequentialGroup()
     .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
     .addGroup(layout.createSequentialGroup()
     .addGap(22, 22, 22)
     .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
     .addComponent(lAmountLabel)
     .addComponent(lAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
     .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
     .addGroup(layout.createSequentialGroup()
     .addGap(69, 69, 69)
     .addComponent(termIntLabel))
     .addGroup(layout.createSequentialGroup()
     .addGap(18, 18, 18)
     .addComponent(termInt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
     .addGap(40, 40, 40)
     .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
     .addComponent(mPayment, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addComponent(mPaymentLabel)))
     .addGroup(layout.createSequentialGroup()
     .addGap(48, 48, 48)
     .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
     .addGroup(layout.createSequentialGroup()
     .addComponent(exitButton)
     .addGap(54, 54, 54)
     .addComponent(calcButton)
     .addGap(87, 87, 87)
     .addComponent(clearButton))
     .addComponent(results, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
     .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
     );
     layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
     .addGroup(layout.createSequentialGroup()
     .addGap(24, 24, 24)
     .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
     .addComponent(lAmountLabel)
     .addComponent(termIntLabel)
     .addComponent(mPaymentLabel))
     .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
     .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
     .addComponent(termInt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addComponent(lAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addComponent(mPayment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
     .addGap(124, 124, 124)
     .addComponent(results, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 165, Short.MAX_VALUE)
     .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
     .addComponent(clearButton)
     .addComponent(calcButton)
     .addComponent(exitButton))
     .addContainerGap())
     );
     
    pack();
     }// </editor-fold>
     
    private void exitButtonActionPerformed(java.awt.event.ActionEve nt evt) {
     System.exit(0);
     }
     
    private void clearButtonActionPerformed(java.awt.event.ActionEv ent evt) {
     lAmount.setText("");
     mPayment.setText("");
     jTextArea1.setText("");
     }
     
     private void calcButtonActionPerformed(java.awt.event.ActionEve nt evt) {
     Integer termInt1;
     termInt1 = termInt.getSelectedIndex();
     if (termInt1 == 0)
     { 
     
    }
     if (termInt1 == 1)
     {
     interest = iRate [0];
     term = years [0];
     }
     if (termInt1 == 2)
     {
     interest = iRate [1];
     term = years [1];
     }
     if (termInt1 == 3)
     {
     interest = iRate [2];
     term = years [2];
     }
     double monthRate = iRate1 /12;
     
     mPayment1 = amount *(monthRate / (1-Math.pow(1 + monthRate, -tMonths)));
     amount = Double.parseDouble(lAmount.getText());
     mPayment.setText(currency.format(mPayment1) + " ");
     repaint();
     tMonths = term * 12;
     balance = amount;
     
     
     for (months = 1; months <= tMonths; months++)
     {
     mRate = iRate1 /12;
     mRatePaid = balance * mRate;
     mPrinciple = mPayment1 - mRatePaid;
     balance = balance - mPrinciple;
     jTextArea1.append(" "+ months + "\t\t " + ePay.format(mPayment1) +"\t\t" + ePay.format(mRatePaid) + "\t\t" + ePay.format (balance) + "\n");
     }
     }
     
     /**
     * @param args the command line arguments
     */
     public static void main(String args[]) {
     /*
     * Set the Nimbus look and feel
     */
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
     /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
     */
     try {
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
     if ("Nimbus".equals(info.getName())) {
     javax.swing.UIManager.setLookAndFeel(info.getClass Name());
     break;
     }
     }
     } catch (ClassNotFoundException ex) {
     java.util.logging.Logger.getLogger(WeekThreeCalc.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
     } catch (InstantiationException ex) {
     java.util.logging.Logger.getLogger(WeekThreeCalc.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
     } catch (IllegalAccessException ex) {
     java.util.logging.Logger.getLogger(WeekThreeCalc.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
     } catch (javax.swing.UnsupportedLookAndFeelException ex) {
     java.util.logging.Logger.getLogger(WeekThreeCalc.c lass.getName()).log(java.util.logging.Level.SEVERE , null, ex);
     }
     //</editor-fold>
     
    /*
     * Create and display the form
     */
     java.awt.EventQueue.invokeLater(new Runnable() {
     
    public void run() {
     new WeekThreeCalc().setVisible(true);
     }
     });
     }
     
     // Variables declaration - do not modify
     private javax.swing.JButton calcButton;
     private javax.swing.JButton clearButton;
     private javax.swing.JButton exitButton;
     private javax.swing.JScrollPane jScrollPane1;
     private javax.swing.JTextArea jTextArea1;
     private javax.swing.JTextField lAmount;
     private javax.swing.JLabel lAmountLabel;
     private javax.swing.JTextField mPayment;
     private javax.swing.JLabel mPaymentLabel;
     private java.awt.ScrollPane results;
     private javax.swing.JComboBox termInt;
     private javax.swing.JLabel termIntLabel;
     // End of variables declaration
     }