Solved

# Loop is not working correctly , Loops right the first time but not the rest--- Frustrated

Posted on 2005-02-26
Medium Priority
267 Views
I am trying to get this program to display 3 different types of Loans in an amoritiization table.  The first loan does what it is supposed to do however the other two loans are not working correctly.  I have been looking at this for hours now and I am about to give up.  I am sure that one of my loops is not correct however, I can not figure this out.  Before you point out that this is homework you are correct, however, I have tried and I did look through many other solutions and tried comparing it to what I have and I still can not get it to work.  Please help, I am sure it is something really stupid that I am missing!!!!

Here is the Program:

import java.io.*;

class Mort4
{                              // program name: Mortgage4

public static void main(String[] arguments) throws IOException {      // start main() function

/*define the variables and declare values where appropriate.  Arrays used to handle 3 options: 7 year loan at 5.35% interest,
*15 year loan at 5.5% interest, and 30 year loan at 5.75% interest
*/

double principal = 200000;                        // principal amount of loan, set to \$200000
double [] interestRateY  = {5.35,5.5,5.75};            // annual interest rate (percentage), array
int  [] termY = {7,15,30 };                        // length of loan in years, array
double balance;
double monthlyPayment = 0;                              // monthly payment, the result of final calculation
int termM = 0;                                    // length of loan in months to be calculated
double interestRateM;                               // monthly interest rate to be calculated
double remainingBalance = principal;                  // remaining balance of loan after payment, initially set to principal
double monthlyInterest = 0;                        // the amount of payment applied to interest, initially set to 0
double monthlyPrincipal = 0;                        // the amount of payment applied to principal, initially set to 0
int counter = 0;                              // display 15 lines of results

// format to display only two decimal places
java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");

// Displays title and basic loan information to user
System.out.println("\n\n\t Mortgage Payment Calculator \n\n" +
"\n Amount of Loan: \t\$" + dec.format(principal));

// loops through the first set of arrays and display interest rate, term, and payment

for ( int i = 0; i< termY.length; i++)      {

// Calculation to find the number of months of the loan (termM)

termM = (termY[i] * 12);

// Calculation to find the monthly interest rate (interestRateM)

interestRateM = (interestRateY[i]/12)/100;

// Calculation to find the amount of the monthly payment

monthlyPayment = (principal * interestRateM) /(1 - Math.pow(1 + interestRateM, - termM));

// Displays monthly mortgage payment based on calculation
System.out.println("\n\nThe monthly payment "  +
"for a " + termY [i] + " year loan at " + interestRateY [i] + " % interest " +
"will be: " +
"\$" + dec.format(monthlyPayment));

} // End for loop
// Pause program so that display can be read and user can continue when they choose

System.out.print("\nPress Enter to Continue");

// Loop to create the amortization table for each of the loans

for ( int i = 0; i< termY.length; i++)      {

// Calculation to find the monthly interest rate (interestRateM)
interestRateM = (interestRateY[i]/12)/100;
// Calculation to find the number of months of the loan (termM)
termM = (termY[i] * 12);

// Calculation to find the amount of the monthly payment

monthlyPayment = (principal * interestRateM)/(1 - Math.pow(1 + interestRateM, - termM));

// Display loan information for the amortization schedule that is being displayed
System.out.println("\n\nThe following is the amortization schedule for a " + termY [i] + " year loan at "

+ interestRateY [i] + "%" + " interest.");

// formats column headers for results to be displayed
System.out.println("\n\n\nPayment\t\tPrincipal\t\tInterest\tRemaining");
System.out.println("Month\t\tPayment\t\t\tPayment\t\tBalance");
System.out.println("---------\t---------\t\t---------\t---------");

// Calculation to find the monthly interest rate (interestRateM)

interestRateM = (interestRateY[i]/12)/100;

// calculation for interest and principal payment
monthlyInterest = (remainingBalance * interestRateM);
monthlyPrincipal = (monthlyPayment - monthlyInterest);

//Loop through the arrays
for(int j = 0; j < termM; j++)
{

// calculation for remaining interest and principal payments
monthlyInterest = (remainingBalance * interestRateM);
monthlyPrincipal = (monthlyPayment - monthlyInterest);
remainingBalance = (remainingBalance - monthlyPrincipal);

// data for display
System.out.println( (j + 1) + "\t\t\$" + dec.format(monthlyPrincipal) +
"\t\t\$" + dec.format(monthlyInterest) +
"\t\t\$" + dec.format(remainingBalance));

counter++;

if(counter == 15)

{
byte[] buffer = new byte[15];
counter = 0;
System.out.println("\n\n\nPayment\t\tPrincipal\t\tInterest\tRemaining");
System.out.println("Month\t\tPayment\t\t\tPayment\t\tBalance");
System.out.println("---------\t---------\t\t---------\t---------");
}
}
}
}

}   // end main
}

0
Question by:gschoser
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 92

Accepted Solution

objects earned 375 total points
ID: 13413265
you need to initialise remainingBalance inside the outer loop.

// calculation for interest and principal payment
monthlyInterest = (remainingBalance * interestRateM);
monthlyPrincipal = (monthlyPayment - monthlyInterest);

remainingBalance = principal;

//Loop through the arrays
for(int j = 0; j < termM; j++)
0

Author Comment

ID: 13414211
Sorry but I do not think that this is the correct answer.  It loops through but it is not giving the correct calculation.  For the first calculation it works fine, however for the 2nd and 3rd calculation it is giving negative numbers.
0

LVL 30

Expert Comment

ID: 13414254
Are you sure you added that line at the right place? I did not get any negative numbers when I executed it (with the line that objects posted).
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
###### Suggested Courses
Course of the Month11 days, left to enroll