Reputation: 53
Okay, I am still new to Java, and I am going on my ninth week of this stuff. I am going to post some source code from a school project - this is NOT the entire source - this chunk of code is my loop to amortize a loan. I am trying to amortize from a menu selection (menu prints to a text field) or from user input. The problem is, and I cannot figure out if it is math, or my loop, is that it is not amortizing the loan properly. I just want to know if anyone sees something obvious that I missed, and if so could you please point it out so I can get on the right track? Thanks in advance!
SOURCE:
private void amortizeButtonActionPerformed( java.awt.event.ActionEvent evt ) {
// This module borrowed from Ryan Jones in George Griepp's PRG 420 class.
NumberFormat nf = NumberFormat.getCurrencyInstance();
int Monthly = 0;
int monthcount = 0;
String Output = "";
int i = 0; // For first loop
double loanamount = Double.parseDouble( tempTextField3.getText() ); //all loan amounts are the same
double rate = Double.parseDouble( tempTextField1.getText() ); //Array for the rate
double time = Double.parseDouble( tempTextField2.getText() ); //Array for the time
double totalnumpayments = 0; //Set for
double monthlypayment = 0; //Set for math calculation in first loop
double interestPayment = 0; //Set for math calculation in first loop
double totaltime = 0; //Set for second loop to know how long to loop
double loan = 0; //Set for second loop
double interestPayment2 = 0; //Set for second loop
double principlePayment = 0; //Set for second loop
for ( i = 0; i < time; i++ ) {//First loop This loops through the arrays and gives the first message listed below three times
monthlypayment = ( loanamount * ( ( rate / 12 ) / ( 1 - Math.pow( ( 1 + ( rate / 12 ) ), -( time * 12 ) ) ) ) );
interestPayment = loanamount * ( rate * 100 / 1200 );
totaltime = ( time * 12 );
jTextArea1.setText( "" );
jTextArea1.setText( "This loan has an interest rate of " + ( rate * 100 ) + "%" + " and a starting loan amount of " + nf.format( loanamount ) );
jTextArea1.setText( "Payment Number\t\t" + "Towards Principle\t\t" + "Towards Interest\t" + "Remaining on loan" );
jTextArea1.setText( "" ); // Part of the first loop this will appear three times with the math listed above
System.out.println( totaltime );
Monthly++;
Output += ( ( monthcount++ ) + "\t\t\t" + nf.format( principlePayment ) + "\t\t\t" + nf.format( interestPayment2 ) + "\t\t\t" + nf.format( loan - principlePayment ) + "\n" );
loan = -principlePayment;// Changes the numbers as the loop goes
interestPayment2 = loan * ( rate * 100 / 1200 );// Changes the numbers as the loop goes
principlePayment = monthlypayment - interestPayment2;// Changes the numbers as the loop goes
}
jTextArea1.setText( Output );
}
Upvotes: 0
Views: 634
Reputation: 36611
There are a few things, which might be an issue or not (I am not familiar with the maths behind loans).
totaltime
in your for
-loop, without making use of the i
variable. Hence it will always result in the same result, and can be calculated outside the loopsetText
on the jTextArea1
inside the loop, where your last call is with the empty String
, you might as well clear it before entering the for
-loop, as in the end the jTextArea1
will just be empty. Or just remove the code since after the for
loop you set the text to Output
loan = -principlePayment
is a weird statement. So if the principlePayment
is a positive number, loan
will be a negative number, and interestPayment2
as wellStringBuilder
to construct your Output
variableI think number 3 is the most important for your calculations.
Upvotes: 2