import java.util.Scanner;
public class FutureInvestment {
	public static void main(String[] args) {
		// Display name
		System.out.println("Programmed by ");
		
		Scanner input = new Scanner(System.in);
		
		// Prompt user to input investment amount
		System.out.print("Enter an investment amount: ");
		double investment = input.nextInt();
		
		// Prompt user to input interest amount
		System.out.print("Enter an interest rate: ");
		double interest = input.nextInt();
		
		// Prompt user to input number of years
		System.out.print("Enter the number of years: ");
		int time = input.nextInt();
		
		// Set values at new variables
		double a = investment;
		double i = interest;
		// Get MONTHLY interest rate from YEARLY interest rate.
		double mi = i / 12;
		int y = time;
		double m = futureInvestmentValue(a,mi,y);
		
		//result
		System.out.println(amount);
	}	
		//Create method for interest rate
		public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){
			int year = 1;
			double amount;
			// Create formula
			while (year <= years)
				amount = investmentAmount * monthlyInterestRate * year;
				year++;
			return amount;
		}
}

my program doesn't seem to work correctly. it says that the amount variable has not been initialized. however, i am at a loss as to what it should be set to. also. when i set amount to "0" the program compiles but when i enter all the values nothing happens and the program still continues to run instead of terminate.

does anyone have any suggestions?
how do you terminate the program if the user enters 0 for any value?

thanks ahead of time!

Edited 6 Years Ago by charat: n/a

it says that the amount variable has not been initialized

Thats cause in the futureInvestmentValue() method if for some reason you 'while' condition is never satisfied, then the amount variable would contain just garbage, hence the compiler is pointing it out to you beforehand by giving that error, just initialise it to 0 and you should be good to go.

when i set amount to "0" the program compiles but when i enter all the values nothing happens and the program still continues to run instead of terminate.

Observe the lines 35,36 and 37 thoroughly:-

// Create formula
			while (year <= years)
				amount = investmentAmount * monthlyInterestRate * year;
				year++;

It actually is:-

// Create formula
			while (year <= years)
				amount = investmentAmount * monthlyInterestRate * year;
			year++;

The year++ is outside your 'while' body. So the 'while' condition is never violated and the program goes into an infinite loop.

It is because of errors such as these the Sun Java Code Conventions always recommends to use brace brackets with all your loops, even if the body may contain a single line of code.

Edited 6 Years Ago by stephen84s: n/a

i just realized i have another problem...

the assignment states that the output should look something like:

The amount invested: 1000
Annual interest rate: 9%
years Future Value
1 1093.80
2 1196.41
...
29 13467.25
30 14730.57

will my program do that? if not, how would i accomplish this?

]ok so i edited my method to look like:

//Create method for interest rate
		public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){
			int year = 1;
			double amount = 0;
			// Create formula
			while (year <= years){
				amount = investmentAmount * monthlyInterestRate * year;
			year++;}
			return amount;
		}

however, it just gives me one answer. how would i be able to make it list out all the progressive answers?

for example, if i entered 5 for the number of years, how would i have it give me the value after year 1, after year 2, after year 3, after year 4, and after year 5?

i also edited my first post to include a System.out.println so that it gives me an actual answer.

Edited 6 Years Ago by charat: n/a

Change the method to return a list or array of the values you want. Each element in the list would be for one year.

i'm not sure how i would go about doing that? can you give me an example?

Here is quick remake of your code showing usage of an array. It would need reworking.

//Create method for interest rate
		public static double[] futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){
			int year = 1;
                        double[] amounts = new double[50]; // make big enough for testing
			double amount = 0;
			// Create formula
			while (year <= years){
				amount = investmentAmount * monthlyInterestRate * year;
                                amouts[year] = amount; // save the amount for this year
			        year++;
                        }
			return amounts;  // return this list of amounts
		}

Edited 6 Years Ago by NormR1: n/a

but what if the user wants to ender over 50? i should be able to account for an infinite amount of numbers.

My code was a simple example. I said: It would need reworking.
an infinite amount of numbers will be hard to do in any computer.

well my assignment REQUIRES that i set it up so it can handle WHATEVER size number i decide to put into it.

SO, you need to think of a solution. We're not here to write your code.

Look at the Collections framework classes. There are several classes that allow you to add items to a collection with no restrictions on how many.

Edited 6 Years Ago by NormR1: n/a

i never asked you to write my code. i merely asked for an example. you just haven't been helpful at all and don't seem to understand what i am trying to do. don't come up with some lame excuse as "we're not here to write your code." just because you don't know how. simply be a man about it and state that you don't know.

i never asked you to write my code. i merely asked for an example. you just haven't been helpful at all and don't seem to understand what i am trying to do. don't come up with some lame excuse as "we're not here to write your code." just because you don't know how. simply be a man about it and state that you don't know.

@charat
NormR1 has a habit of obfuscating his answers so that people new to Java like yourself do some exploring on their own. He already has given you sample code on how to use Arrays and pointed you in the direction of achieving your goal of 'infinite amount of numbers', unfortunately your pride has taken over and instead of looking at the pointers to solve your problem, you are getting overworked on nothing.

So be a man about it, and learn to spot what you need, after all if you do not get your project done, its not going to matter to him but yourself only.

BTW This is all you had to do to get your answer.

I would suggest using a for loop instead of that while loop, just adding my 2 cents to the story :). Good Luck!

Edited 6 Years Ago by WargRider: n/a

This article has been dead for over six months. Start a new discussion instead.