Need help making program work!

Program hangs in first loop

Page 1 of 1

5 Replies - 5762 Views - Last Post: 18 March 2006 - 03:41 PM Rate Topic: -----

#1 kjava91  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 27-February 06

Need help making program work!

Posted 11 March 2006 - 06:47 PM

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 re-write 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. :crazy:


/*
* 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();}
}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Need help making program work!

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Need help making program work!

Posted 12 March 2006 - 12:35 AM

Well the problem is in your math, not the loops.

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.
Was This Post Helpful? 0
  • +
  • -

#3 kjava91  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 27-February 06

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 :D !
Just that simple change made all the diffence in the world.
Was This Post Helpful? 0
  • +
  • -

#4 Xtrema  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-September 05

Re: Need help making program work!

Posted 13 March 2006 - 07:27 PM

dont u hate it when that happens? one simple line? :P

gotta love the feeling when everything falls into place tho :)
Was This Post Helpful? 0
  • +
  • -

#5 lennyj67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-March 06

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.
Was This Post Helpful? 0
  • +
  • -

#6 Xtrema  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-September 05

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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1