# How to add loop statement

• 02-24-2013, 09:34 PM
Hawks
How to add loop statement
Hi everyone,

I am in need of finishing up my problem. Before I ask my questions here is the problem:

Write a method that computes future investment value at a given interest rate for a specified number of years.
Use the following method header:

Attachment 4564

Write a test program that prompts the user to enter the investment amount (e.g., 1000) and the interest rate (e.g., 9%) and prints a table
that displays future value for the years from 1 to 30, as shown below:
Attachment 4563

I need to create a loop to show years (1-30), as I already have gotten the other codes. Here is what I have so far.

Code:

```public static void main(String[] args) {                 // Step 0: create a Scanner for input         Scanner input = new Scanner(System.in);                 // Step 1: enter investment amount         System.out.print("Enter investment amount: ");         double investmentAmount = input.nextDouble();                 // Step 2: enter annual interest rate         System.out.println("Enter annual interest Rate, e.g. 4.25: ");         double annualInterestRate = input.nextDouble();                         // Step 3: calculate monthly rate from annual interest rate         double monthlyInterestRate = annualInterestRate / 1200 ;                         // Step 4: enter number of years         System.out.print(                 "Enter number of years as an integer, for example 1: ");         int numberofYears = input.nextInt();                 // Step 5: compute future value         double futureInvestmentValue = investmentAmount * Math.pow(1 + (annualInterestRate * .01                 /12),numberofYears * 12);                 // Step 6: output future value         System.out.println("Future value with interest compunded monthly:" + "\$"                 + futureInvestmentValue + ".");             } }```
Thanks guys!
• 02-24-2013, 10:16 PM
DarrylBurke
Re: How to add loop statement
Quote:

Originally Posted by Hawks
Before I ask my questions ...

Did you forget to ask your questions?

db
• 02-24-2013, 10:18 PM
Hawks
Re: How to add loop statement
Sorry if I was confusing. My question is how would I start/make the loop to display the 30 future value results.
• 02-24-2013, 10:56 PM
AndrewM16921
Re: How to add loop statement
First you'd want some method to calculate the future value for a single year. Then, you loop from 0 to 30 and call that method as needed. Something like this:

Code:

```public static void main(String[] args) {         double[] amount = new double[30];         double[] annual = new double[30];         double[] monthly = new double[30];         //fill data from input...                 System.out.println("Years \t\t Future Value");         for(int i = 0; i < 30; i++)         {                 System.out.println(i + " \t\t " + futureValue(amount[i], annual[i], monthly[i], i));         } } public static double futureValue(double amount, double annual, double monthly, int years) {         double value = 0.0;         // calculate future value based on data         return value; }```
• 02-24-2013, 11:24 PM
Hawks
Re: How to add loop statement
Thank you!