javamath

Need help Java amortization table calculations


This is my homework. I finally got the months to display right but the amounts are wrong. Also, not necessary but it would be nice to stop and Print something between each loan. Like loan 1, loan 2, loan 3...

Any help will be appreciated

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

I have the months correct but the loan amounts are wrong.

    import java.io.IOException;      //Code that delays ending the program


   class MonthlyRhondav4
    {      
  
  public static void main ( String[] args) throws IOException{
     
     double loanAmount = 200000.00;   // $ amount borrowed
     double monthlyPayment = 0;   // monthly payment for calculating
     double loanBalance;
     double interestPaid;
     double principalPaid;
     int paymentCounter;
     int lineCounter = 0;
    

               
     java.text.DecimalFormat dcm = new java.text.DecimalFormat("$,###.00");                                      
  
            
     int termArray[] = {84, 180, 360};      // Different loan terms in months 
     double interestArray[] = {0.0535, 0.055, 0.0575};// Different interest rates for the loan
     int k =0;// gonna be paymentIndex
        
  /*Code to start the payment list*/   
   
     System.out.print("\n\nPlease Press Enter to Continue to the 3 Different    Amortization Lists");
     System.out.println ();
     System.out.println ();
  
     System.in.read();
     System.in.read();  
     
  /*Display columns*/    
    
     System.out.println("Month \t Loan Amount Left\tInterest\t\tPrincipal \n"); //Prints headers for columns
     System.out.println ();
                    
              
  /*Loop to calculate and print monthly payments*/
  
     //for(k=0; k<3; k++){// k is going to be paymentIndex to loop through index
     
     for (k = 0; k < interestArray.length; k++) {
     
        for(paymentCounter =1; paymentCounter <= termArray[k]; paymentCounter++)  // months through array
        { 
        
        /********TROUBLE HERE***************************************************************************************/
        
           monthlyPayment = ((loanAmount * (interestArray[k]) * termArray[k]) + loanAmount) / (termArray[k] * 12);

           interestPaid = loanAmount*(interestArray[k]/12);            //interest paid through array
           
           principalPaid = monthlyPayment-loanAmount*(interestArray[k]/12);       //principal paid
      
      /*need to fig monthly payment+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      
      
                   
          
           System.out.println(paymentCounter + "\t" + dcm.format(loanAmount) + "\t\t" + dcm.format(interestPaid) + "\t\t\t" + dcm.format(principalPaid));
        
           
           
           lineCounter++;                                //Increment the display counter
           if (lineCounter > 11 && paymentCounter < termArray[k]*12) //Check to see if 12             
           {
                   
              System.out.println ("Please Press Enter to Continue the List" ); //Code to delay ending the program
              System.in.read();
              System.in.read();
              lineCounter = 0; 
                         
           }
        
        }
        loanAmount = (loanAmount - (monthlyPayment-loanAmount*(interestArray[k]/12))); //Calculate new loan amount
     }
  
  
  }//ends public static void main 
     }//ends public class 

Solution

  • One observation -- you're not doing anything with the loan balance. The reason why nothing is changing is that having computed the interest and principal amounts of a given payment, you're not reducing the loan balance by the principal portion of the payment. You need to change your code to display the current loan balance and to compute the principal/interest split from the current loan balance and not the original loan amount.


    Edited

    Ok -- I see you were trying to update the balance, but you have it outside the loop for the loan. That needs to be inside the loop so that it is updated for each payment. Also, you have things like loanAmount * (interestArray[k] / 12) over and over again. Consider using variables, such as

    double interestPaid = loanAmount * (interestArray[k] / 12)
    

    This will make your code easier to read and more maintainable since if you find a mistake in the calculation, you only have to fix the mistake in one place rather than having to fix it everywhere you had the calculation.

    I also don't see where you're calculating the monthly payment. That's a function of the original loan amount, number of payments, and interest rate. Remember, the monthly payment is fixed and the interest/principal split of each payment will change as the loan is paid down. You might find http://en.wikipedia.org/wiki/Mortgage_calculator useful to figure out the formula for the monthly payment.