Results 1 to 6 of 6
  1. #1
    janalyzer is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default Output does not dispay as intended -- HELP!!

    I am seriously at my wits end!!:confused::mad: I have been working on this for days, and since it is homework assignment I am on a deadline! (11:59 PM MST tonight)

    My program is supposed to get a loan amount from a textbox, have the user select a rate and term with radio buttons - then calculate and display the mortgage amortization schedule. The formulas seem to work Ok, as they do provide the expected output; however my display only hows one line, which shows the payment number as the last expected payment;; all other information shows calculations for the first line of the amortization schedule.

    Please look, any help would be much appreciated. Here is my code. (created in netbeans ide if anyone wants to know)

    /*
    * MortgCalculatorWk3.java
    *
    * Created on Jan 22, 2009, 2:15:48 PM
    * Author: Janelle Klepic
    * Created for PRG 421 class
    * Instructor: Marico Howe
    *
    * The purpose of this program is to create a simple GUI program asking for user to
    enter a loan amount, and select an interest rate and term from a provided
    list of radiobuttons.
    * If the Calculate Payment button is clicked, the program calculates and displays
    * a paymemt number, paymemt amount, interest paid, principle paid and new balance for term of loan
    * basicaly an amortization schedule
    * Clear and Exit buttons are also provided to clear the form or to exit the program
    *
    */
    import java.io.*;
    import java.math.*;
    import java.text.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane;
    */
    public class JanWeek3 extends javax.swing.JFrame {

    double rate = 0, term = 0;
    double [][] loanInfo = //declare array for loan info (term and rate respectively)
    {
    {7, 5.35},
    {15, 5.5},
    {30, 5.75}
    }; //close of array

    /** Creates new form JanWeek3 */
    public JanWeek3() {
    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">//GEN-BEGIN:initComponents
    private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    week3 = new javax.swing.JPanel();
    loanAmt = new javax.swing.JTextField();
    LoanAmtLabel = new javax.swing.JLabel();
    directionsLabel = new javax.swing.JLabel();
    loan1 = new javax.swing.JRadioButton();
    loan2 = new javax.swing.JRadioButton();
    loan3 = new javax.swing.JRadioButton();
    hidden = new javax.swing.JRadioButton();
    clearForm = new javax.swing.JButton();
    calculate = new javax.swing.JButton();
    exit = new javax.swing.JButton();
    resultsLabel = new javax.swing.JLabel();
    resultsText = new java.awt.TextArea();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setTitle("Jan's Week 3 Nightmare!");

    week3.setBorder(javax.swing.BorderFactory.createEt chedBorder());

    LoanAmtLabel.setText("Desired Loan Amount");

    directionsLabel.setText("Please select loan terms from list below ");

    buttonGroup1.add(loan1);
    loan1.setText("7 years at 5.35%");
    loan1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    radioButtonActionPerformed(evt);
    }
    });

    buttonGroup1.add(loan2);
    loan2.setText("15 years at 5.5%");
    loan2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    radioButtonActionPerformed(evt);
    }
    });

    buttonGroup1.add(loan3);
    loan3.setText("30 years at 5.75%");
    loan3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    radioButtonActionPerformed(evt);
    }
    });

    buttonGroup1.add(hidden);
    hidden.setSelected(true);
    hidden.setText("youcantseeme!");
    hidden.setVisible(false);
    hidden.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    radioButtonActionPerformed(evt);
    }
    });

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

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

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

    resultsLabel.setText("Results of Query");
    resultsLabel.setEnabled(false);
    resultsText.setEnabled(false);

    javax.swing.GroupLayout week3Layout = new javax.swing.GroupLayout(week3);
    week3.setLayout(week3Layout);
    week3Layout.setHorizontalGroup(
    week3Layout.createParallelGroup(javax.swing.GroupL ayout.Alignment.LEADING)
    .addGroup(week3Layout.createSequentialGroup()
    .addGroup(week3Layout.createParallelGroup(javax.sw ing.GroupLayout.Alignment.LEADING)
    .addGroup(week3Layout.createSequentialGroup()
    .addGroup(week3Layout.createParallelGroup(javax.sw ing.GroupLayout.Alignment.LEADING)
    .addGroup(week3Layout.createParallelGroup(javax.sw ing.GroupLayout.Alignment.LEADING)
    .addGroup(week3Layout.createParallelGroup(javax.sw ing.GroupLayout.Alignment.LEADING)
    .addGroup(week3Layout.createSequentialGroup()
    .addComponent(loanAmt, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(LoanAmtLabel)
    .addGap(65, 65, 65))
    .addGroup(week3Layout.createSequentialGroup()
    .addComponent(directionsLabel)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)))
    .addGroup(week3Layout.createSequentialGroup()
    .addComponent(loan1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)))
    .addGroup(week3Layout.createSequentialGroup()
    .addComponent(loan2)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)))
    .addGroup(week3Layout.createParallelGroup(javax.sw ing.GroupLayout.Alignment.LEADING)
    .addComponent(clearForm)
    .addComponent(exit)
    .addComponent(calculate))
    .addGap(446, 446, 446))
    .addComponent(loan3)
    .addComponent(resultsLabel)
    .addComponent(hidden)
    .addComponent(resultsText, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap())
    );

    week3Layout.linkSize(javax.swing.SwingConstants.HO RIZONTAL, new java.awt.Component[] {loan1, loan2, loan3});

    week3Layout.linkSize(javax.swing.SwingConstants.HO RIZONTAL, new java.awt.Component[] {calculate, clearForm, exit});

    week3Layout.setVerticalGroup(
    week3Layout.createParallelGroup(javax.swing.GroupL ayout.Alignment.LEADING)
    .addGroup(week3Layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(week3Layout.createParallelGroup(javax.sw ing.GroupLayout.Alignment.BASELINE)
    .addComponent(loanAmt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(LoanAmtLabel)
    .addComponent(exit))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addGroup(week3Layout.createParallelGroup(javax.sw ing.GroupLayout.Alignment.BASELINE)
    .addComponent(clearForm)
    .addComponent(directionsLabel))
    .addGroup(week3Layout.createParallelGroup(javax.sw ing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, week3Layout.createSequentialGroup()
    .addGap(18, 18, 18)
    .addComponent(calculate)
    .addGap(105, 105, 105))
    .addGroup(week3Layout.createSequentialGroup()
    .addGap(5, 5, 5)
    .addComponent(loan1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(loan2)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(loan3)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(hidden)
    .addGap(16, 16, 16)
    .addComponent(resultsLabel)))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(resultsText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(139, 139, 139))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(week3, javax.swing.GroupLayout.PREFERRED_SIZE, 462, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(49, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(week3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addContainerGap())
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void radioButtonActionPerformed(java.awt.event.ActionEv ent evt) {//GEN-FIRST:event_radioButtonActionPerformed
    Object source = evt.getSource();
    if (source == loan1){
    term = (loanInfo)[0][0];
    rate = (loanInfo)[0][1];
    } //end first if
    else if (source == loan2){
    term = (loanInfo) [1][0];
    rate = (loanInfo) [1][1];
    }//end second if
    else if (source == loan3){
    term = (loanInfo) [2][0];
    rate = (loanInfo) [2][1];
    }//end third if
    else if (source == hidden){
    rate = 0;
    term = 0;

    }//end last if for radio bitton selection
    }//GEN-LAST:event_radioButtonActionPerformed

    private void clearFormActionPerformed(java.awt.event.ActionEven t evt) {//GEN-FIRST:event_clearFormActionPerformed
    hidden.setSelected(true);
    loanAmt.setText("");
    resultsText.setText("");
    resultsLabel.setEnabled(false);
    resultsText.setEnabled(false);
    }//GEN-LAST:event_clearFormActionPerformed

    private void calculateActionPerformed(java.awt.event.ActionEven t evt) {

    double principal, mpayment =0;
    double mIntPaid = 0; // calculate current monthly interest = orig loan amt * monthly interest rate
    double mPrincPaid = 0;// calculate montlyh princ paid = payment - current monthly innterest
    double newBal = 0; // calcukate new balance = orig loan amount - month princ paid
    int paymentNum = 0;//set principal = new balance - go in a loop

    //validate text entry of loan amount
    principal = Double.parseDouble(loanAmt.getText()); //conversion of text entry of loan amount
    //validate user selelction of rate and term

    if (term == 0 || rate == 0)
    { JOptionPane.showMessageDialog(null, "Invaild Entry! You must select a loan rate and term!!", "ERROR", JOptionPane.ERROR_MESSAGE);
    }//close of if statement


    term = term * 12; //change length of term from years to months
    rate = rate/(12* 100); //calculate monthly interest rate into decimal form
    mpayment = principal * rate / (1 - Math.pow(1 + rate, -term)); // Perform Calculation of Mortgage payment
    mpayment = Math.rint( mpayment * 100.0d ) / 100.0d; //round the newly calculalted payment to two decimal places

    resultsLabel.setEnabled(true);
    resultsText.setEnabled(true);

    //perform calculations of monthly interest, monthly principal paid & new prinicpal balance
    //all this will be done inside a loop

    for (paymentNum = 1; paymentNum <= term; paymentNum++) {

    mIntPaid = principal * rate;
    mIntPaid = Math.rint( mIntPaid * 100.0d ) / 100.0d; // round of monthly interest
    mPrincPaid = mpayment - mIntPaid;
    mPrincPaid = Math.rint(mPrincPaid * 100.0d ) / 100.0d;
    newBal = principal - mPrincPaid;
    principal = newBal;

    //set text area ready and displau result!!
    resultsText.setText((String.valueOf(paymentNum)) + " " + (String.valueOf(mpayment))
    + " " + (String.valueOf(mIntPaid)) + " " + (String.valueOf(mPrincPaid)) + " "
    + (String.valueOf(newBal)));

    }//end of loop


    }//End of calculate


    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
    System.exit(0);
    }//GEN-LAST:event_exitActionPerformed

    private void resultsTextTextValueChanged(java.awt.event.TextEve nt evt) {//GEN-FIRST:event_resultsTextTextValueChanged
    // TODO add your handling code here:
    }//GEN-LAST:event_resultsTextTextValueChanged

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new JanWeek3().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LoanAmtLabel;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton calculate;
    private javax.swing.JButton clearForm;
    private javax.swing.JLabel directionsLabel;
    private javax.swing.JButton exit;
    private javax.swing.JRadioButton hidden;
    private javax.swing.JRadioButton loan1;
    private javax.swing.JRadioButton loan2;
    private javax.swing.JRadioButton loan3;
    private javax.swing.JTextField loanAmt;
    private javax.swing.JLabel resultsLabel;
    private java.awt.TextArea resultsText;
    private javax.swing.JPanel week3;
    // End of variables declaration//GEN-END:variables

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This post is often called a "here's my code, fix it for me" or a "homework dump".

    You may get some decent answers, and I hope that you do, but you'll have better luck getting your questions answered if you would first do a little debugging, isolate where the problem is and then just post the code relevant to the problem, preferably in compilable state.

    Best of luck.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    some suggestions though:

    You're using a TextArea object which is an AWT component instead of the Swing equivalent, the JTextArea. In general (and in this situation), it's a bad idea to mix AWT and Swing. So change your TextArea to be a JTextArea.

    Next: you're finding everything is being added to one line of the text area, and that's because you're using the setText(...) method. This method replaces any text in the area with the new String. You want to use instead the append(...) method that adds your String to the Strings already in the text area.

    You'll also need to add an "\n" to your String to give it a line feed, and you'll need to read up on String formatting.

    edit: you'll also want to place that JTextArea component into a JScrollPane component so you can scroll to see the entire result.

    Best of luck.
    Last edited by Fubarable; 01-25-2009 at 03:19 AM.

  4. #4
    janalyzer is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you for the input, I will try the suggestion of the JTextArea instead. I also made an attemt at the new line with an append which did not work as I had hoped. So I have had a break and some food and hopefully that with the advice from you will help!:)

    Also, one thing, I realized it may have looked like a homework dump, but I am terribly new, this being my second GUI app ever after only a handful of Java programs. I was hoping someone besides my absent instructor could give me a bit of advice. Thanks

  5. #5
    janalyzer is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    if it were possible I would kiss your feet!
    I took your advice, made some changes, added better formatting for my strings and it works like a charm and looks beautiful! Thanks so much for pointing me in the right direction!!!

  6. #6
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Output does not dispay as intended -- HELP!!

    At least You could put CODE tags.

Similar Threads

  1. what the outPut
    By alksam in forum Advanced Java
    Replies: 5
    Last Post: 12-25-2008, 01:44 PM
  2. different output
    By pro85 in forum Java Applets
    Replies: 6
    Last Post: 08-05-2008, 01:42 PM
  3. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 10:14 PM
  4. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 04:05 AM
  5. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 10:34 PM

Posting Permissions

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