Array loop not resetting

Posted on 2006-05-01
Last Modified: 2008-03-17
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.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("\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));


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

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

                  // formating used to set column headers for results

                  catch (InterruptedException e)
                  }      // end if
                  // end else
            }// end while
    }              // end main
}// end MtgCalc
Question by:antcal
    LVL 92

    Accepted Solution

    >                balance = principal;               // set current balance = principal

    move that line inside your first loop
    LVL 8

    Expert Comment

     >>Double interestRateYears[]={5.35, 5.5, 5.75};

    this should be

     double interestRateYears[]={5.35, 5.5, 5.75};

    Author Comment

    Thanks ALOT objects! It finally works!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now