1. Member
Join Date
May 2014
Posts
1
Rep Power
0

Java 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 fake
{
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);
}
}
}
}
}```
Last edited by cb3131; 05-28-2014 at 11:29 PM.

2. ## Re: Need help fixing very trivial definite loop problem - please help. Will take a m

[code]
[/code]
to get highlighting and preserve formatting.

3. Senior Member
Join Date
Feb 2014
Posts
219
Rep Power
3

## Re: Need help fixing very trivial definite loop problem - please help. Will take a m

Please describe the problem - current behaviour, and expected behaviour.

4. ## Re: Need help fixing very trivial definite loop problem - please help. Will take a m

The placing of the {} and the lack of indentation of nested code makes the code very hard to read and understand.
Can you fix it to make it readable?

5. Senior Member
Join Date
Feb 2014
Posts
447
Rep Power
3

## Re: Need help fixing very trivial definite loop problem - please help. Will take a m

The two loops with k an i does not really make sense.
I is <= 12
K starts with i and loops while k<=12 - so it is only executed when i=12.
(so you could replace the for loop with if (i==12)).

So my suggestion is to split up the code into smaller methods that you can check more easily.

6. Member
Join Date
May 2014
Posts
5
Rep Power
0

## Re: Need help fixing very trivial definite loop problem - please help. Will take a m

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

You need to change this and replace the for loop with a while loop or just use plain old IF statement

if(k==12){
//Then do something
}

The check for the for loop is executed only when the value of i is 12.