Update....I added the .java with GUI file

Java GUI loan payment calculator

Posted 30 April 2009 - 04:15 PM

I am creating a GUI using GUI builder in NetBeans and I have to have the user enter the Price, Down Payment, Interest Rate (based on which jradio button selected) and Length of Loan. I have to then calculate the monthly payment based on these 4 things. The interest rate is either 5, 10 or 12 percent. I have everything done except for the calculation which I am confused on (not very good at math) and I believe that I need to add some variable designations. Any suggestions? Thanks.

Replies To: Java GUI loan payment calculator

Re: Java GUI loan payment calculator

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// First we define float variables.
float price, down, interest, length, result;
// We have to parse the text to a type float.
price = Float.parseFloat(jTextField1.getText());
down = Float.parseFloat(jTextField2.getText());
interest = Float.parseFloat(jTextField3.getText());
length = Float.parseFloat(jTextField4.getText());
// Now we can perform the calculation
interest = interest / 100 / 12;
float ipow = (float)java.lang.Math.pow(1 + interest, length);
interest = (interest * ipow) / (ipow - 1);
result = (price - down) * interest;
// We will now pass the value of result to jTextField3.
// At the same time, we are going to
// change the value of result from a float to a string.
jTextField5.setText(String.valueOf(result));
}

This seems to work for me. I removed the % sign as the parse will fail. It didn't fail for you, because you were getting interest from the wrong textbox. You may want to add some error trapping for user input, but basically this is how it is done. I also assumed the length was months.