Array loop not resetting

I am having a bit of problem resetting my values from my array back to default for the second and third run. Where must I fix it? I am new and this has consummed my life!!! lol.

      import java.io.*;
      import java.text.*;

              public class MtgCalc5
       {
             public static void main(String[] args) throws IOException
                  {
                        DecimalFormat decimalPlaces=new DecimalFormat("0.00");

                        double monthlyPayment=0;
                        double principal;
                        double balance=0;
                        double monthlyInterestPayment=0;
                        double monthlyPrincipalPayment;
                        double interestRateMonths;
                        int termMonths = 0;
                        int linecount;


                  //assignment of Array values

                  Double interestRateYears[]={5.35, 5.5, 5.75};
                  int termYears[]={7,15,30};

                  //assignment of fixed values
                  principal = 200000.0;
                  balance = principal;                  // set current balance = principal
                  monthlyInterestPayment = 0;            // set current interest payment = 0
                  monthlyPrincipalPayment = 0;            // set current principal payment = 0
                  linecount = 50;                  // display 50 lines of results

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

                        // calculation of monthly payment
                        principal = 200000.0;
                        termMonths = termYears[i] * 12;
                        interestRateMonths = (interestRateYears[i] / 12) / 100;
                        monthlyPayment = (principal * interestRateMonths) / (1 - Math.pow(1 + interestRateMonths, - termMonths));

            // formating used to set column headers for results
                              System.out.println("\n\n\n\n");
                              System.out.println("\tYour loan amount of $200,000.00");
                              System.out.println("\tWith an interest rate of" + (interestRateYears[i]));
                              System.out.println("\tMonthly Payment of $" + decimalPlaces.format(monthlyPayment));
                              System.out.println("\n\n");

                              System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
                              System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
                              System.out.println("---------\t---------\t---------\t---------");


                  /// start while loop
            while (termMonths > 0)
            {
            // calculate interest and principal payments

                   monthlyInterestPayment = (balance * interestRateMonths);
                   monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
                   balance = (balance - monthlyPrincipalPayment);


            // format column headers for results to be displayed
                              System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
                              "\t$" + decimalPlaces.format(monthlyInterestPayment) +
                              "\t\t$" + decimalPlaces.format(balance));

                  // decrement months
                         termMonths--;

            // these conditional statements cause the results to pause
                    if(linecount == 50)
                    {
                  linecount = 0;
                  try
                  {
                  Thread.sleep(3000);      // pause to last three seconds

                  // formating used to set column headers for results
                        System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
                        System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
                        System.out.println("---------\t---------\t---------\t---------");
                        }

                  catch (InterruptedException e)
                  {
                  }
                  }      // end if
                  else
                  {
                  linecount++;
                  }
                  // end else
            }// end while
    }              // end main
}
}// end MtgCalc
antcalAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
>                balance = principal;               // set current balance = principal

move that line inside your first loop
0
 
Manikandan ThiagarajanSenior consultantCommented:
 >>Double interestRateYears[]={5.35, 5.5, 5.75};

this should be

 double interestRateYears[]={5.35, 5.5, 5.75};
0
 
antcalAuthor Commented:
Thanks ALOT objects! It finally works!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.