Tags: amortization, asilly, calculation, chart, class, java, loan, loop, programming

Loan Calculation Loop

On Java Studio » Java Programming

6,202 words with 4 Comments; publish: Thu, 07 Feb 2008 15:15:00 GMT; (15093.75, « »)

Hi all, I'm new to Java & taking my first class in programming.

I have been trying to get a

silly loan amortization chart to show. I need help with the

loop part I believe (or control statement). I need my loan balance to automatically update after each payment.

A friend told me to try something like:

totalLoanBalance = (TotalLoan Balance+NewLoanBalance) - payment

Please help me , I have been monkeying with this since Friday evening and I'm about ready to throw java (and all programming)

down the drain.

My code is as follows:

/**

* Title: Amortization Schedule

* Description: A program to let the user enter the loan amount, number of years, and interest rate, then displays the amortization schedule for the loan.

* Copyright: Copyright (c) 2002

* Company: Duke Court

* .java-program.developerfaqs.com.author Mary Davenport

* .java-program.developerfaqs.com.version 1.0

*/

public class amortization

{

//Main Method

public static void main(String[] args)

{

//Define variables

double annualIntRate, principal, balance, loanAmount, years, principalAmtPaid;

//Prompt for loan amount

System.out.print("Enter loan amount, for example 120000.95: ");

loanAmount = MyInput.readDouble();

//Prompt for number of years

System.out.print("Enter number of years: ");

years = MyInput.readDouble();

//Prompt for annual interest rate, //Obtain monthly interest rate

System.out.print("Enter Annual Interest Rate as an integer, for " +

"example 5.0: ");

annualIntRate = MyInput.readDouble();

double monthlyIntRate = annualIntRate/1200;

//Calculate monthly payment & total payments & //Display the monthly payment

double monthlyPymt = loanAmount*monthlyIntRate/

(1-1/Math.pow(1 + monthlyIntRate, years*12));

System.out.println("The monthly payment is " +

(int)(monthlyPymt*100)/100.0);

//Calculate the monthly interest payment //Display the monthly interest payment

double monthlyIntPymt = (loanAmount*monthlyIntRate);

System.out.println("The monthly interest payment is " +

(int)(monthlyIntPymt*100)/100.0);

//Calculate the monthly principal payment, //Display the monthly principal payment

double monthlyPrincipalPymt = (monthlyPymt - monthlyIntPymt);

System.out.println("The monthly principal payment is " +

(int)(monthlyPrincipalPymt*100)/100.0);

//Calculate the loan balance, //Display the loan balance

double loanBalance = (loanAmount - monthlyPrincipalPymt);

System.out.println("The loan balance is " +

(int)(loanBalance*100)/100.0);

//display the number of payments, //calculate the number of total payments

double numberOfPymts = (years*12);

System.out.println("The total number of payments is: " +

numberOfPymts);

//Display the amortization table

System.out.print("Payment #" + "\t" + "Interest" + "\t" + "Principal" +

"\t" + "Balance" + "\n" );

int pymtNumber;

for (pymtNumber = 1; pymtNumber <= (years*12); pymtNumber++)

//doesn't work: for (pymtNumber = 1; pymtNumber <= (years*12) - (years*12) -1; pymtNumber++)

{

// if(monthlyPrincipalPymt < loanBalance)

{

System.out.print(pymtNumber + "\t\t" + (int)(monthlyIntPymt*100)/100.0);

System.out.print("\t\t" + (int)(monthlyPrincipalPymt*100)/100.0);

System.out.println("\t\t" + (int)(loanBalance*100)/100.0);

}

}

}

}

All Comments

Leave a comment...

  • 4 Comments
    • You're assuming that we know what it means to "update the loan balance after each payment". Could you explain what that means?

      If you could explain the logic, someone would be able to show you how to code it.

      Also it helps if you put some eye catching comments in your code to help find the place where the problem. Something like: ***HERE IS THE PROBLEM***

      and then reference them in you question so Find can be used to go directly there.

      To change the value of a numeric variable (var) by amt you could code:

      var = var - amt; // reduce var by amt

      #1; Sat, 10 Nov 2007 02:39:00 GMT
    • Thanks Norm for the suggestion.

      Ok, I'll try to explain the logic.

      the loan balance must be "updated" by subtracting the amount of principal paid with each succeeding principal payment.

      The amount of principal paid = loan balance* monthly interest rate

      This is the result I get with my current code:

      Enter loan amount, for example 120000.95: 10000.00

      Enter number of years: 1

      Enter Annual Interest Rate as an integer, for example 5.0: 7.0

      The monthly payment is 865.26

      The monthly interest payment is 58.33

      The monthly principal payment is 806.93

      The loan balance is 9193.06

      The total number of payments is: 12.0

      Payment # Interest Principal Balance

      1 58.33 806.93 9193.06 (this line is right)

      2 58.33 806.93 9193.06(this & following

      3 58.33 806.93 9193.06(lines needs to

      4 58.33 806.93 9193.06(re-figured after

      5 58.33 806.93 9193.06(each principal

      6 58.33 806.93 9193.06(amount pd)

      7 58.33 806.93 9193.06

      8 58.33 806.93 9193.06

      9 58.33 806.93 9193.06

      10 58.33 806.93 9193.06

      11 58.33 806.93 9193.06

      12 58.33 806.93 9193.06

      #2; Sat, 10 Nov 2007 02:40:00 GMT
    • The variable holds the amount(loanBalance) is not being decremented. You need to subtract an amount from it somewhere.

      Write a small program to test decrementing a variable to see how to do it. For example:

      int amt = 123; // The starting amount

      int pp = 12; // the amount do decrement by each loop

      int cntr = 0; // loop counter

      while (amt > 0) {

      amt = amt - pp; // decrement the amount

      System.out.println("loop: " + cntr++ + " amt=" + amt); // show results and increment counter

      }

      #3; Sat, 10 Nov 2007 02:41:00 GMT
    • Norm,

      I'm trying that right now.

      Thanks for getting me on track!

      Mary

      #4; Sat, 10 Nov 2007 02:42:00 GMT