Hello, I am hoping to get some advice on what I might be doing wrong. This is the second program I wrote but I am still very new at writing any JAVA code so please bear with me.

Instructions are - Create a Mortgage Calculator to display the original loan amount, current interest rate, principal paid, and remaining balance. If the list scrolls off of screen, use loops to display a partial list,
hesitate, and then display more of the list.

A list prints but no calculations are not displayed. Here is what it looks like - and it does not break. I am sure it has to do with the way I am calling for calculations or maybe I am missing a "{" or something else.

Any suggestions will be greatly appreciated.

run:
Month1
Monthly Payment isNaNInterest Paid isNaNPrincipal Paid isNaNRemaining Balance isNaNMonth2
Monthly Payment isNaNInterest Paid isNaNPrincipal Paid isNaNRemaining Balance isNaNMonth3
Monthly Payment isNaNInterest Paid isNaNPrincipal Paid isNaNRemaining Balance isNaNMonth4
Monthly Payment isNaNInterest Paid isNaNPrincipal Paid isNaNRemaining Balance isNaNMonth5

``````public class CathyAlomariMortgageCalculator
{
private static Object[] remaningBalance;
public static void main(String[] args)

{

//main method starts here
//variables defined

double monthlyPayment = 1167.15;

double remainingBalance = 200000.00;

double interestPaid = remainingBalance * 0.0575/12;

double principalPaid = monthlyPayment - interestPaid;

remainingBalance = remainingBalance - principalPaid;

//loop starts here

for(int counter=1;counter <= 30*12;counter++)

{
System.out.println("Month"+ counter);

monthlyPayment =1167.15;
float \$ = 0;
float n = 0;
System.out.printf("Monthly Payment is"+\$%.2f/n,monthlyPayment);

interestPaid = remainingBalance * 0.0575/12;
System.out.printf("Interest Paid is" + \$%.2f/n,interestPaid);

principalPaid = monthlyPayment-interestPaid;
System.out.printf("Principal Paid is" + \$%.2f/n,principalPaid);

remainingBalance = remainingBalance-principalPaid;
System.out.printf("Remaining Balance is" + \$%.2f/n,remaningBalance);

}
int limit = 11;
int count = 1;
while (count < limit){

}
}

}

//end program
``````

Actually I got it running by removing...

Very unlikely!

If you remove `public static void main(String[] args)` your program won't run at all.
`for(counter%10==0){...` will ignore the counter variable and loop forever, just executing sleep after sleep. Since this is the last thing in the program it will just …

There are a few reasons why it does not go to 0.

1. You are displaying 361 months instead of 360. You have already computed the interest, capital, and remaining balance before you go into the loop. That's the first month but you did not display it.
2. The payment per …

## All 6 Replies

Actually I got it running by removing

`````` public static void main(String[] args)

float \$ = 0;
float n = 0;
``````

Changing the while statement to

``````for(counter%10==0){
{
``````

Only problme is it does not end in a ZERO balance. Something about I truncated the payment....
What's that?

Actually I got it running by removing...

Very unlikely!

If you remove `public static void main(String[] args)` your program won't run at all.
`for(counter%10==0){...` will ignore the counter variable and loop forever, just executing sleep after sleep. Since this is the last thing in the program it will just stop your program from ever trminating normally.

Maybe you should post your actual latest code.

Actually I mentiond the wrong code. I meant to say that I removed this....

`````` private static Object[] remaningBalance;
``````

I'll try to get my code back up to see what I might be doing wrong.

Thanks

Here is the new code but it just keeps running. I tried using a while statement for the counter but it still kept running and running...

And then someone told me I needed to use a whole number because I "truncated" the payment and that's why it does not end in a zero balance.

``````public class CathyAlomariMortgageCalculator
{

public static void main(String[] args) throws InterruptedException

{

//main method starts here
//variables defined

double monthlyPayment = 1167.15;

double remainingBalance = 200000;

double interestPaid = remainingBalance * 0.0575 / 12;

double principalPaid = monthlyPayment - interestPaid;

remainingBalance = remainingBalance - principalPaid;

//loop starts here

for(int counter=1;counter <= 30*12;counter++)

{
System.out.println("Month"+ counter);

monthlyPayment = 1167.15;
System.out.printf("Monthly Payment is:\$%.2f/n",monthlyPayment);

interestPaid = remainingBalance * 0.0575 / 12;
System.out.printf("Interest Paid is:\$%.2f/n",interestPaid);

principalPaid = monthlyPayment - interestPaid;
System.out.printf("Principal Paid is:\$%.2f/n",principalPaid);

remainingBalance = remainingBalance - principalPaid;
System.out.printf("Remaining Balance is:\$%.2f/n",remainingBalance);

if (counter%10==0) {

}

}
{

}
}

}

//end main method
//end program
``````

There are a few reasons why it does not go to 0.

1. You are displaying 361 months instead of 360. You have already computed the interest, capital, and remaining balance before you go into the loop. That's the first month but you did not display it.
2. The payment per month number is a hard-coded which is supposed to be from an equation that rounds up the number. If you use 1167.1457128871 as your payment per month, you would get the answer much closer to 0. In banking (finance), they round it up to pennies (2 decimals), so it ends up with 1167.15.

Edit: Line 31, you do not need to redefine the value for monthly payment. You never change its value in the loop, so you could simply remove it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.