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

• 01-25-2009, 02:10 AM
janalyzer
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
* 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();
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 ");

loan1.setText("7 years at 5.35%");
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

loan2.setText("15 years at 5.5%");
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

loan3.setText("30 years at 5.75%");
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

hidden.setSelected(true);
hidden.setText("youcantseeme!");
hidden.setVisible(false);
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

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

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

exit.setText("Exit");
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.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(
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
);
layout.setVerticalGroup(
);

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

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

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
}//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.JTextField loanAmt;
private javax.swing.JLabel resultsLabel;
private java.awt.TextArea resultsText;
private javax.swing.JPanel week3;
// End of variables declaration//GEN-END:variables

}
• 01-25-2009, 03:58 AM
Fubarable
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.
• 01-25-2009, 04:09 AM
Fubarable
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.
• 01-25-2009, 04:24 AM
janalyzer
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
• 01-25-2009, 06:12 AM
janalyzer
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!!!
• 01-24-2012, 06:14 AM
diamonddragon
Re: Output does not dispay as intended -- HELP!!
At least You could put CODE tags.