I cannot make my code loop.... Anyone please help!

/*
PRG420
Week 2 Individual Project
Amanda Cornwell
September 10, 2007
MortgageProgramCornwell.java
This program will calculate and display the
monthly mortgage payment amount, given the amount
of the mortgage, term of the mortgage, and the intrest
rate of the mortgage.
Next, the program will display the monthly mortgage payment
amount.  Then, list the loan balance and interest paid for
each payment over the term of the loan. It should also
hesitate during displaying the list.
*/


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



public class MortgageProgramCornwell
{
public static void main(String[] args) throws IOException
{
//constants
int principal = 200000;
float fraction = 0;
int months = -360;
double monthlypayment = 0;



//calculation
fraction = (float) 575 / 120000;
index= (float) principal * ((fraction / (1-(Math.pow((1+fraction),(months))))));


DecimalFormat df = new DecimalFormat ("####.##");
// output
System.out.println("The Principal is " + principal);
System.out.println("The Intrest Rate is 5.75%");
System.out.println("The Number of Years is 30 years");
System.out.println("The Monthly Payment is " + (df.format(monthlypayment)));


//constants
double monthly_principal=0;
double monthly_interest = 0;
double remainbalance= 0;
int lineCount= 20;
remainbalance= (principal - monthly_principal);


while(principal > 0)
System.out.println("Balance is " + (df.format(remainbal)));
System.out.println("Interest paid is $" + (df.format(monthly_interest)));


monthly_interest= (principal * fraction);
monthly_principal= (monthlypayment - monthly_interest);


//Pause Screen
if(lineCount > 0) {
lineCount--;
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
}
}
}
//Stops Loop
if(remainbalance <=0){
System.out.println("The loan balance is: 0.00");
}}
}

Edited 3 Years Ago by happygeek: fixed formatting

You been just little messy with bracklets, I think you want that code something like this

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

class MortgageProgramCornwell
{
	public static void main(String[] args) throws IOException
	{
		//constants
		int principal = 200000;
		float fraction = 0;
		int months = -360;
		double monthlypayment = 0;
		
		
		//calculation
		fraction = (float) 575 / 120000;
		double index= (float) principal * ((fraction / (1-(Math.pow((1+fraction),(months))))));
		
		DecimalFormat df = new DecimalFormat ("####.##");
		// output
		System.out.println("The Principal is " + principal);
		System.out.println("The Intrest Rate is 5.75%");
		System.out.println("The Number of Years is 30 years");
		System.out.println("The Monthly Payment is " + (df.format(monthlypayment)));
		
		//constants
		double monthly_principal=0;
		double monthly_interest = 0;
		double remainbalance= 0;
		int lineCount= 20;
		remainbalance= (principal - monthly_principal);
		
		while(principal > 0)
		{
			System.out.println("Balance is " + (df.format(remainbalance)));
			System.out.println("Interest paid is $" + (df.format(monthly_interest)));
		
			monthly_interest= (principal * fraction);
			monthly_principal= (monthlypayment - monthly_interest);
			
			//Pause Screen
			if(lineCount > 0) {
				lineCount--;
				try {
				Thread.sleep(1500);
				} catch (InterruptedException e) {
				}
			}
			//Stops Loop
			if(remainbalance <=0){
			System.out.println("The loan balance is: 0.00");
			}
		}
	}
}

But still you have to sort your while loop as right now it is infinitive, principal doe not change

Thank you for the help... I am still a little lost. I have made the changes, however I cannot figure out how to code the change to principal... I am also still getting about 4 error codes.

w

hile ( principal > 0 )
{
System.out.println("Balance is " + (df.format(remainbal)));
System.out.println("Intrest paid is " + (df.format(monthly_interest)));


remainbal = (remainbal - monthly_principal);
monthly_interest= (principal * fraction);
monthly_principal=(monthlypayment - monthly_interest);


//Pause Screen
if(lineCount > 0) {
lineCount--;
Thread.sleep(1500);
} catch (InterruptedException e)  {


}
}
//Stop Loop
if(remainbal <=0)  {
System.out.println("The loan balance is : 0.00");
}
}
}

It will not show all the loan payments... I think I am missing something major. I have checked both textbooks and cannot figure out for the life of me the problem. Any help is greatly appreciated.

Edited 3 Years Ago by happygeek: fixed formatting

Make sure you put your code to be executed each iteration inside parentheses:

while (principle > 0) {
    code line 1
    code line 2
    ...........
}

otherwise, only the first line after the while statement will be executed each loop

Lets analyse your requirements

Program have to display:

  • monthly mortgage payment
  • mortgage amount
  • term of mortgage
  • interest rate

I see you use

  • morgage as principal = 200000;
  • interest rate is 5.75%
  • mortgage lenght for 30 years or 360months

I do not understand why you declared int months = -360; as negative number.
I have no idea what is fraction for fraction = (float) 575 / 120000; and why you use 120000?
Calculation of index double index= (float) principal * ((fraction / (1-(Math.pow((1+fraction),(months)))))); I expect to by calculation of monthly pyement, however I'm not sure about that formula you use.

So you need to sort this part and display as mentioned in firts four bullet points.

Next step, program have to display:

  • monthly mortgage payment (which we already know, from previous calculation)
  • loan balance (I think this should show how much you already paid)
  • paid interest (shod be that extra money which you have to pay for the mortgage you got, so taken that mortgage is 200000 on 360 months with 5.75% interest, payment without interest would be something like 555.56. With interest 555.56x1.0575 = 587.5. Monthly interest would be 587.5- 555.56 = 31.94 which will sum with previously paid interests)
  • on top of this program have to hesitate for short time, which you did corectly with time thread.

The whole think have to loop for 360 times as you declared mortgage period, so that is what your while loop should be based, also instead of while you can use for loop

PS: If I misunderstand your requirements, my apology as english is not my first language and provided requirements are not realy helpful, if it was me I would ask more question to get clear image of it.

Principal is not changing because you are not changing it... It should not be changed by it self.
To explain your loop, you are telling the loop to continue WHILE principal will be greater than 0. At the start you are telling the compiler that principal=2000. And after than you have not made anything like principal=(some calculation). So, it is infinitive loop.
As you mentioned the loop should run for 360 times (30 years), then you know when to stop. Run it for 360 times. Your while loop should have a condition like:

month=0;
amortization_period=30; //30 years
while(month<=(amortization_period*12))
{
   //do whatever you are doing
   //if anything goes wrong get out from the loop
   month++;
}

I hope it should answer your question!
Regards!

This article has been dead for over six months. Start a new discussion instead.