# Investment Calc

• 02-28-2010, 11:25 AM
rfore1
Investment Calc
Spent hours on this. Help please.

Have an assignment as follows:

Write a program that prompts the user for an initial investment amount and a goal investment amount and calculate how many years it will take to grow from the initial amount to the goal amount with a fixed interest rate (ie: 5 %). (use the for loop)

Prompt the user if they would like to run the program with other amounts or quit. (use the while loop). Output the initial amount, the goal amount and the number of years to reach that amount.

Cannot get it to work. Below is what I have. Any suggestions would be greatly appreciated.

import static java.lang.System.out;
import java.util.Scanner;
import java.io.*;

public class Project_5 {//main
public static void main(String[] args) {

double principle = 0;//initial amount investing
double interest = 0;
double rate = 0.05;//the fixed interest amount
double years = 0;//amout of years it will take to achieve goal

Scanner myScanner = new Scanner(System.in);

System.out.println("****************************** ********* ");
System.out.println("* Welcome to the Investment Calculator * ");
System.out.println("****************************** ********* ");

System.out.println ("Enter your initial investment amount: if you want to exit enter 0.");
inputNumber = myScanner.nextInt();
principle = inputNumber;

if (inputNumber == 0){//if num = 0 exit class

System.exit(0);
}

System.out.println ("Enter your goal investment amount: ");
inputNumber2 = myScanner.nextInt ();
goal = InputNumber2;

System.out.println ("The fixed interest rate is 5%");

for (years = 0; years < goal; years++){
interest = principal * rate;
sum = sum + years;
{
System.out.print("The number of years you must invest after giving \$ " + (goal);
System.out.println("is ") + (years) + (" years");
}

}
}
}
• 02-28-2010, 12:40 PM
JosAH
Quote:

Originally Posted by rfore1
for (years = 0; years < goal; years++){

This doesn't make sense; suppose I want to have 1000 bucks somewhere in the future, i.e. goal == 1000. It doesn't make sense to loop 1000 years ...

Just do you maths: my initial deposit is 'principal' bucks and the interest rate is 'interest' (say 5%) so after one year my total amount of money is:

total= (1+interest)*principal

so after two years my amount of money is:

total= (1+interest)*total.

etc. etc. You should keep on looping until total >= goal.

kind regards,

Jos
• 02-28-2010, 01:00 PM
rfore1
for loop
I see....so how and where do I place this in the program. Do I replace the for loop?....

• 02-28-2010, 02:12 PM
JosAH
Quote:

Originally Posted by rfore1
I see....so how and where do I place this in the program. Do I replace the for loop?....

Of course because the current one is incorrect. I read your private message; let's solve this step by step; first we want to make our amount of money grow until we've reached a goal (an amount of money):

Code:

```total= principal; // start with our initial amount of money for (; total < goal; total= (1+interest)*total);```
This loop simply goes on and on until the 'goal' amount of money is reached. The number of years that are needed equals the number of times the (now) empty body of the loop executes; we have to count those:

Code:

```int years= 0; // not looped yet total= principal; // start with our initial amount of money for (; total < goal; total= (1+interest)*total)   years++; // add one more loop iteration```
There, that's all there is to it.

kind regards,

Jos
• 02-28-2010, 03:19 PM
JosAH

kind regards,

Jos
• 02-28-2010, 03:29 PM
rfore1
Kind of new to this so didn't know.

I finally got the compile and got it to run.

It lets me input the initial investment amount and the goal investment amount but then stops running and doesn't go through the loop.

Here is what prints:

***************************************
* Welcome to the Investment Calculator *
***************************************
Enter your initial investment amount: if you want to exit enter 0.
1000
20000
The fixed interest rate is 5%
_________________________________________

Something is wrong with this section of the program:

total= principal;
for (; total < goal; total= (1+interest)*total);
years++;{
//interest = principal * rate;
//sum = sum + years;
System.out.print("The number of years you must invest after giving \$ ");
System.out.print(goal);
System.out.print(" is");
System.out.println(years);
System.out.println(" years");

Thanks for all of your help.
• 02-28-2010, 03:37 PM
JosAH
Quote:

Originally Posted by rfore1
for (; total < goal; total= (1+interest)*total);

What is that semi-colon doing there at the end of the line? That way you've created an empty for-body and you don't want that.

kind regards,

Jos
• 02-28-2010, 04:23 PM
rfore1
still stuck. Looks like the for loop format might not be right. The setup should look something like this format, right?

for (initialization; termination; increment)

would this be right.. for (years=0; total < goal; years++)
• 02-28-2010, 04:45 PM
JosAH
Quote:

Originally Posted by rfore1
still stuck. Looks like the for loop format might not be right. The setup should look something like this format, right?

for (initialization; termination; increment)

would this be right.. for (years=0; total < goal; years++)

That is correct; my version is also correct; all three of the for header parts are optional. Ultimately:
Code:

`for(;;)`
... would be correct as well. Can you show us your code as it is now?

kind regards,

Jos
• 02-28-2010, 06:55 PM
rfore1
Still having issues with this....I get so close and then have another issue.

The program got all the way to the for loop and it begins an infinite loop.

The loop kept running until it crashed...

The program looked like this...

***************************************
* Welcome to the Investment Calculator *
***************************************
Enter your initial investment amount: if you want to exit enter 0.
1000
5000
The fixed interest rate is 5%
The number of years you must invest after giving \$1000 is 3361 years.

You could see it running through the loop up to that number and then freezing.

Let me know what you think.
• 02-28-2010, 06:58 PM
JosAH
Quote:

Originally Posted by rfore1
Still having issues with this....I get so close and then have another issue.

The program got all the way to the for loop and it begins an infinite loop.

The loop kept running until it crashed...

The program looked like this...

***************************************
* Welcome to the Investment Calculator *
***************************************
Enter your initial investment amount: if you want to exit enter 0.
1000
5000
The fixed interest rate is 5%
The number of years you must invest after giving \$1000 is 3361 years.

You could see it running through the loop up to that number and then freezing.

Let me know what you think.

I think that I want to see your code.

kind regards,

Jos
• 02-28-2010, 07:07 PM
rfore1
Here is what my entire code looks like:

Code:

```import static java.lang.System.out; import java.util.Scanner; import java.io.*; public class Project_5 {//main public static void main(String[] args) {//begins body double principal = 0;//initial amount investing double interest = 0; double rate = 0.05;//the fixed interest amount int years = 0;//amout of years it will take to achieve goal double goal = 0; double total = 0; Scanner myScanner = new Scanner(System.in); System.out.println("*************************************** "); System.out.println("* Welcome to the Investment Calculator * "); System.out.println("*************************************** "); System.out.println ("Enter your initial investment amount: if you want to exit enter 0."); int inputNumber = myScanner.nextInt(); principal = inputNumber; if (inputNumber == 0){//if num = 0 exit class System.exit(0); } System.out.println ("Enter your goal investment amount: "); int inputNumber2 = myScanner.nextInt (); goal = inputNumber2; System.out.println ("The fixed interest rate is 5%");  total= principal;  total= (1+interest)*total;     for (years=0; total < goal; years++)      //interest = principal * rate;     //sum = sum + years;   {  System.out.print("The number of years you must invest to meet your goal of \$ ");     System.out.print(goal);     System.out.print(" is");            System.out.println(years);     System.out.println(" years");         }   }   }```
Moderator edit: color tags changed to code tags
• 02-28-2010, 07:21 PM
Fubarable
Moderator edit above: color tags changed to code tags. For more on this, click on the link in my signature below. Thanks and good luck.
• 02-28-2010, 07:21 PM
JosAH
Still that loop doesn't make any sense, reread my previous replies; also check your 'fixed' interest rate. It is zero in your code instead of 5%.

kind regards,

Jos