public class FutureInvestmentValue {
public static void main(String[] args) {
Scanner input= new Scanner (System.in);
System.out.print("Enter investment amount: ");
double investmentAmount = input.nextDouble();
System.out.print("Enter annual interest rate: ");
double annualInterestRate = input.nextDouble();
System.out.print("Enter the number of years: ");
double years = input.nextDouble();
double monthlyInterestRate = (annualInterestRate)/1200;
double investmentValuepart1 = (1 +monthlyInterestRate);
double investmentValuepart2 = (years *12);
double investmentValuepart3 = Math.pow(investmentValuepart1, investmentValuepart2);
double futureInvestmentValue = investmentAmount*investmentValuepart3;
System.out.printf("Accumulated value is: %.2f"+futureInvestmentValue);
}
}
the inputs work fine but when this program is displaying futureInvestmentValue, it gives an error. can someone help find the error?
yes, someone can check it -- you!
Run the dang program and see whether it produces the correct result!
this is the assignment question:
Write a program that reads in investment amount, annual interest rate, and number of years, and displays the future investment value using the following formula:
and displays the future investment value using the following formula:
futureInvestmentValue =
investmentAmount * (1 + monthlyInterestRate)numberOfYears*12
For example, if you enter amount 1000, annual interest rate 3.25%, and number of years 1, the future investment value is 1032.98.
Hint: Use the Math.pow(a, b) method to compute a raised to the power of b.
Check for commas, colons and extra spaces : )
I got the error, it was:
System.out.printf("Accumulated value is: %.2f"+futureInvestmentValue);
the plus sign that is in front of futureInvestmentValue should be comma like
System.out.printf("Accumulated value is: %.2f",futureInvestmentValue);
can someone the code if its good?
public class FutureInvestmentValue {
public static void main(String[] args) {
Scanner input= new Scanner (System.in);
System.out.print("Enter investment amount: ");
double investmentAmount = input.nextDouble();
System.out.print("Enter annual interest rate: ");
double annualInterestRate = input.nextDouble();
System.out.print("Enter the number of years: ");
double years = input.nextDouble();
double monthlyInterestRate = (annualInterestRate)/1200;
double investmentValuepart1 = (1 +monthlyInterestRate);
double investmentValuepart2 = (years *12);
double investmentValuepart3 = Math.pow(investmentValuepart1, investmentValuepart2);
double futureInvestmentValue = investmentAmount*investmentValuepart3;
System.out.printf("Accumulated value is: %.2f", futureInvestmentValue);
}
}
The error in your code is located in the `System.out.printf()` statement. The correct syntax for printing the value would be:
```java
System.out.printf("Accumulated value is: %.2f", futureInvestmentValue);
```
Note that the `%f` is used as a placeholder for the floating-point value (`futureInvestmentValue`) and the format specifier `%.2f` is used to specify that the value must be displayed with two decimal places.
In your code, you used concatenation (`+`) instead of a comma (`,`) to separate the format string and the value. Using concatenation like `"%.2f" + futureInvestmentValue` would result in a `String` instead of the expected format.