# Thread: While loops - Having problems

1. Member Join Date
May 2011
Posts
1
Rep Power
0

## While loops - Having problems

I have a project for school where we are to calculate the monthly payments of a \$200,000 at 5.75% over 30 years. I was able to get this part working with my program.

The next step was to display the monthly loan balance and interest paid for each month over the term of the loan. Using a WHILE loop I'm able to display the loan balance and interest but it displays the same number every month. I do not know how to use the new loan balance in the loop calculation. Any help would greatly be appreaciated. My program is below.

package mortgagepaymentcalculator;
import java.text.NumberFormat;
/**
*
* @author Owner
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Calculate the monthly mortgage payment of a \$200,000 loan over
// a 30 year team at 5.75%
int principal = 200000; // total of loan
NumberFormat nf = NumberFormat.getCurrencyInstance();
double rate = .0575; // interest rate
int months = 12; // months in a year
int payments = 360; // total number of payments
double percent = rate/12; //To get the interest rate per month
double interest = Math.pow(1 + percent, payments); //calculates 1 + percent to the n power

double monthlypayments = principal * (percent * interest)/(interest - 1); //calculates monthly payments

System.out.println("The monthly payements for a loan of \$" + principal);
System.out.println("at percentage rate of " + rate);
System.out.println("with payments totalling " + payments);
System.out.println("would be " + nf.format(monthlypayments));

double loanbalance = 200000;

int count = 1; //initializes the counter for the while loop
while (count <=360) { //starts the while loop

//double monthlyinterest = 0;
//double monthlyprincipal = 0;

double monthlyinterest = (loanbalance * percent); //calculates interest per month
double monthlyprincipal = (monthlypayments - monthlyinterest); //calculates monthly principal applied to loan
loanbalance = principal - monthlyprincipal; //calculates latest monthly principal

System.out.println("The new loan balance for this month will be " + nf.format(loanbalance));
System.out.println ("The interest paid this month is " + nf.format(monthlyinterest));

count++;} //increments each cycle by one

}
}  Reply With Quote

2. Senior Member Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0

## my geuss is this line

Java Code:
`loanbalance = principal - monthlyprincipal; //calculates latest monthly principal`
should be something like this
Java Code:
`loanbalance -= monthlyprincipal;`
Are principal and loanbalance the same thing? That could be a problem also and may cause this not to work.
Last edited by yellowledbet; 05-16-2011 at 08:14 AM.  Reply With Quote

#### Posting Permissions

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