Link to home
Start Free TrialLog in
Avatar of gschoser
gschoser

asked on

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

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

        BufferedReader in = new BufferedReader(
        new InputStreamReader(System.in)); {


/*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");
            in.readLine();

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

            for ( int i = 0; i< termY.length; i++)      {
                  //balance=principal; //added by gretchen


                // 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)

                    {
                        System.out.print("\nPress RETURN To Continue:");
                        byte[] buffer = new byte[15];
                        System.in.read(buffer);
                        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
}  

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gschoser
gschoser

ASKER

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.
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).