Welcome to Dream.In.Code
Become a Java Expert!

Join 150,385 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,157 people online right now. Registration is fast and FREE... Join Now!




endless loop

 
Reply to this topicStart new topic

endless loop

greenhorn100
8 Feb, 2008 - 09:30 PM
Post #1

New D.I.C Head
*

Joined: 24 Jan, 2008
Posts: 7

My program compiles but I have created an endless loop...would anyone have any reccomendations. Thanks.


/*
* Mortgagecalculator5.java
*
*
* 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
*
*
*/


CODE

import java.text.*;
import java.io.*;

public class MortgageCalculator5{

public static void main(String[] args){
//Declares and builds the variable of the loan amount
int loanAmount = 200000;
double loanBalance = 200000;
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=loanBalance-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();


interestPaid = monthlyPayment * interest[0];}

//Displays loop with second mortgage and interest paid
loanBalance = 200000;
if (i == 2)
while (loanBalance > 0){
//Declares formulas
monthlyPayment = (loanAmount*(interest[1]/12)) / (1 - 1 /Math.pow((1 + interest[1]/12), loanTerm[1]));
loanBalance=loanBalance-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
loanBalance=200000;
if (i == 3)
while (loanBalance > 0){
//Declares formulas
monthlyPayment = (loanAmount*(interest[2]/12)) / (1 - 1 /Math.pow((1 + interest[2]/12), loanTerm[2]));
loanBalance=loanBalance-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();}
}
}
}

User is offlineProfile CardPM
+Quote Post

capty99
RE: Endless Loop
8 Feb, 2008 - 10:03 PM
Post #2

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,259



Thanked: 16 times
Dream Kudos: 550
My Contributions
don't let the while loop go on forever?

the first loop doesn't modify the loanbalance in the loop so it goes on forever.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month