Results 1 to 14 of 14
Thread: Investment Calc
 02282010, 11:25 AM #1Member
 Join Date
 Dec 2009
 Posts
 10
 Rep Power
 0
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
double goal = 0;//amount wanting to acquire goal:mad::mad:
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");
}
}
}
}
 02282010, 12:40 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
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
 02282010, 01:00 PM #3Member
 Join Date
 Dec 2009
 Posts
 10
 Rep Power
 0
for loop
I see....so how and where do I place this in the program. Do I replace the for loop?....
Thanks for your help..
 02282010, 02:12 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
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):
Java Code:total= principal; // start with our initial amount of money for (; total < goal; total= (1+interest)*total);
Java 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
kind regards,
Jos
 02282010, 03:19 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
@OP: I read your private message again; please post your problems in here and not through private messages.
kind regards,
Jos
 02282010, 03:29 PM #6Member
 Join Date
 Dec 2009
 Posts
 10
 Rep Power
 0
Sorry about that
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
Enter your goal investment amount:
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.
 02282010, 03:37 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
 02282010, 04:23 PM #8Member
 Join Date
 Dec 2009
 Posts
 10
 Rep Power
 0
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++)
 02282010, 04:45 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
 02282010, 06:55 PM #10Member
 Join Date
 Dec 2009
 Posts
 10
 Rep Power
 0
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
Enter your goal investment amount:
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.
 02282010, 06:58 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
 02282010, 07:07 PM #12Member
 Join Date
 Dec 2009
 Posts
 10
 Rep Power
 0
Here is what my entire code looks like:
Java 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"); } } }
Last edited by Fubarable; 02282010 at 07:20 PM. Reason: Color tags changed to code tags

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.
 02282010, 07:21 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
Bookmarks