Will someone please take a look at the following code and tell me why the program is getting stuck in the first loop. I have tried to rewrite this program a million ways and I keep coming up with the same answer. It's gets stuck in the first loop, does the mathmatical equation and prints it over and over. I need the each of the terms to loop through until the terms hit zero then cycle to the next term until all three have displayed. Thanks.
/*
* Mortgagecalculator.java
*
* Created on March 08, 2006
* Version 4.0
* To calculate and display the mortgage of the hard coded terms
* amount = $200,000, then the term will be 7 years @ 5.35%, 15 years @ 5.5%, or 30 years @ 5.75%
* Each term will be displayed with its monthly payment, loan balance, and interest paid on the
* loan until the each note has the balance of zero
*
*
*/
import java.text.*;
public class MortgageCalculator{
public static void main(String[] args){
//Declares and builds the variable of the loan amount
int loanAmount = 200000;
double loanBalance = 0;
double interestPaid = 0;
int loanTerm[] = {84, 180, 360};
double interest[] = {.0535, .0550, .0575};
double monthlyPayment = 0;
DecimalFormat fmt = new DecimalFormat("0.##");
//Starts array loop
for (int i = 0; i < 3; i++) {
//Declares formula
monthlyPayment = (loanAmount*(interest[i]/12)) / (1  1 /Math.pow((1 + interest[i]/12), loanTerm[i]));
//Displays loop with first mortgage and interest paid
if (i == 1)
monthlyPayment = (loanAmount*(interest[0]/12)) / (1  1 /Math.pow((1 + interest[0]/12), loanTerm[0]));
loanBalance = loanAmount  monthlyPayment;
interestPaid = monthlyPayment * interest[0];
while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms
System.out.println("Mortgage Calculator");
System.out.println("The loan amount is $200,000");
System.out.println("The term of the loan is " + loanTerm[0]);
System.out.println("The monthly payment is $" + fmt.format(monthlyPayment));
System.out.println("The loan balance is: $" + fmt.format(loanBalance));
System.out.println("The interest paid for the loan is $" + fmt.format(interestPaid));
System.out.println();
loanBalance = loanAmount  monthlyPayment;
interestPaid = monthlyPayment * interest[0];}
//Displays loop with second mortgage and interest paid
if (i == 2)
while (loanBalance > 0){
//Declares formulas
monthlyPayment = (loanAmount*(interest[1]/12)) / (1  1 /Math.pow((1 + interest[1]/12), loanTerm[1]));
loanBalance = loanAmount  monthlyPayment;
interestPaid = monthlyPayment * interest[1];
//Displays Mortgage Calculator with the first terms until balance equals zero
System.out.println("Mortgage Calculator");
System.out.println("The loan amount is $200,000");
System.out.println("The term of the loan is " + loanTerm[1]);
System.out.println("The monthly payment is $" + fmt.format(monthlyPayment));
System.out.println("The loan balance is: $" + fmt.format(loanBalance));
System.out.println("The interest paid for the loan is $" + fmt.format(interestPaid));
System.out.println();}
//Displays loop with third mortgage and interest paid
if (i == 3)
while (loanBalance > 0){
//Declares formulas
monthlyPayment = (loanAmount*(interest[2]/12)) / (1  1 /Math.pow((1 + interest[2]/12), loanTerm[2]));
loanBalance = loanAmount  monthlyPayment;
interestPaid = monthlyPayment * interest[2];
//Displays Mortgage Calculator with the first terms until balance equals zero
System.out.println("Mortgage Calculator");
System.out.println("The loan amount is $200,000");
System.out.println("The term of the loan is " + loanTerm[2]);
System.out.println("The monthly payment is $" + fmt.format(monthlyPayment));
System.out.println("The loan balance is: $" + fmt.format(loanBalance));
System.out.println("The interest paid for the loan is $" + fmt.format(interestPaid));
System.out.println();}
}
}
}
5 Replies  5925 Views  Last Post: 18 March 2006  03:41 PM
Replies To: Need help making program work!
#2
Re: Need help making program work!
Posted 12 March 2006  12:35 AM
Well the problem is in your math, not the loops.
If you look at the formula, you are subtracting constant numbers. You initialized the loanAmount variable to 20000 and you know that the monthly payment is 1167.15.
So your formula looks like this:
198832.85 = 200000  1167.15
Since nothing in your formula is changing in value in each iteration of the loop. You have just created an endless loop. The condition of your while statment is that loanBalance must eventually reach less than zero for it to quit the loop.
What you need to do after inizializing loanAmount is to initialize loanBalance to be equal to the original loanAmount. You would do this in your variable declaration, not inside the loop. Then change your equation to something like this:
Now each iteration through the loop will subtract the previous amount stored in loanBalance and the monthlyPayment. And then store the new lower value in loanBalance, eventually it will get to zero.
Now since you are using the same variable name in all 3 mortgages. You will have to reset the loanBalance to equal the original loan amount variable before entering each new loop. Otherwise the 2 and 3rd mortgages will never execute because loanBalance will already be less than zero.
loanBalance = loanAmount  monthlyPayment;
If you look at the formula, you are subtracting constant numbers. You initialized the loanAmount variable to 20000 and you know that the monthly payment is 1167.15.
So your formula looks like this:
198832.85 = 200000  1167.15
Since nothing in your formula is changing in value in each iteration of the loop. You have just created an endless loop. The condition of your while statment is that loanBalance must eventually reach less than zero for it to quit the loop.
What you need to do after inizializing loanAmount is to initialize loanBalance to be equal to the original loanAmount. You would do this in your variable declaration, not inside the loop. Then change your equation to something like this:
loanBalance = loanBalance  monthlyPayment;
Now each iteration through the loop will subtract the previous amount stored in loanBalance and the monthlyPayment. And then store the new lower value in loanBalance, eventually it will get to zero.
Now since you are using the same variable name in all 3 mortgages. You will have to reset the loanBalance to equal the original loan amount variable before entering each new loop. Otherwise the 2 and 3rd mortgages will never execute because loanBalance will already be less than zero.
#3
Re: Need help making program work!
Posted 12 March 2006  08:34 PM
Thank you so much! Once again with your help I have been able to get my program to work. I can not thank you enough !
Just that simple change made all the diffence in the world.
Just that simple change made all the diffence in the world.
#4
Re: Need help making program work!
Posted 13 March 2006  07:27 PM
dont u hate it when that happens? one simple line?
gotta love the feeling when everything falls into place tho
gotta love the feeling when everything falls into place tho
#5
Re: Need help making program work!
Posted 18 March 2006  08:11 AM
I need help with this java program:
Project: Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:
7 year at 5.35%
15 year at 5.5%
30 year at 5.75%
Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.
Project: Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:
7 year at 5.35%
15 year at 5.5%
30 year at 5.75%
Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.
#6
Re: Need help making program work!
Posted 18 March 2006  03:41 PM
from what i understand this is not the place where you just post an assignment and expect someone to do it for u.
if you havent already give it a shot, if you cant get it to work, post ur code and im sure people would be glad to help out. But that isnt the way to ask for it. Besides, you can only get better by practicing
if you havent already give it a shot, if you cant get it to work, post ur code and im sure people would be glad to help out. But that isnt the way to ask for it. Besides, you can only get better by practicing
Page 1 of 1
