# Need help fixing very trivial for loop problem

• May 29th, 2014, 07:48 PM
cb3131
Need help fixing very trivial for loop problem
Code :

```// conor brennan // cb3131 // 5/27/14 // this program gives a monthly report of how much the person should be saving/ the balance of their account   import java.util.Scanner;   public class savingsplan2 { public static void main(String args []) { Scanner kb = new Scanner(System.in);   System.out.println("What is your savings goal in dollars?"); //prompt user double goal = kb.nextDouble();   System.out.println("Enter the expected annual rate of return in percent: "); //prompt user double monthlyrate = kb.nextDouble() / 1200;   System.out.println("Enter time until goal in years: "); //prompt user double time = kb.nextDouble();   double pv = 1 / Math.pow(1 + monthlyrate, time * 12); double save = (goal * pv * monthlyrate / ( 1 - pv));   int savedollar = (int) save; // integer value of how much should be saved //per month double savecent = (save - savedollar) * 100; // number of cents left over int savecent1 = (int)savecent; // finds integer value of cents left over double savetotal = savedollar + (double)savecent1 / 100; // adds integer //of dollars and // decimal of //cents to find //monthly payment //to nearest cent System.out.println(); System.out.println("Savings goal: " + goal); System.out.println("Annual rate of return: " + monthlyrate * 12 + "%"); System.out.println("monthly rate: " + monthlyrate + "%"); System.out.println("Time until goal: " + time + " years"); System.out.println("to reach this goal, you must save \$" + savetotal + " per month"); System.out.println(); double interest = 0; double balance = 0; double totalinterest = 0; double finalinterest = 0; for (int j = 1; j <= 3; j++){ // prints header for each year long chart System.out.println(); System.out.println("Savings schedule for year " + j); System.out.println(); System.out.println("month interest amount balance"); System.out.println();       for (int i = 1; i <= 12; i++) { // prints values in the chart for 3 years double amount = savetotal; interest = balance * monthlyrate ; // finds monthly interest int realinterest = (int)interest; // gets rid of numbers after decimal double realinterest1 = (interest - realinterest) * 100; // gets value after //decimal double actualinterest = (int)realinterest1; // gets rid of numbers after //decimal finalinterest = actualinterest / 100 + realinterest; // finds interest to two //decimal places balance = amount + balance + finalinterest; // accumulates balance totalinterest = totalinterest + finalinterest; System.out.print(i); System.out.println(" " + finalinterest + " " + amount + " " + balance);     for (int k = i; k >= 12; k--) { // prints footer at end of each year long //chart System.out.println(); System.out.println("Savings summary for year " + j); System.out.println("Total amount saved: " + (12 * amount)); System.out.println("Total interest earned: " + totalinterest); System.out.println("End of year balance: " + balance); } } } } }```

the program is doing everything I want it to do but one thing. I want it to accumulate the interest earned per year and to print that number. By that I mean that I want the interest for year one to be printed after year one, and then I want the interest to reset to 0. I want the interest earned to be only for one individual year at a time. My program currently accumulates the interest each consecutive year, so by year 3, the printed interest is the accumulation of interest earned in year 1, 2, and 3.
• May 29th, 2014, 08:07 PM
Norm
Re: Need help fixing very trivial for loop problem