# Java Calculate Future Investment Amount

• 09-11-2012, 05:04 AM
rjanisze
Java Calculate Future Investment Amount
I do not know where I'm going wrong here. Pleaqse take a look and let me know what suggestions you have. Thank you. #So Frustrated

import java.util.Scanner;

public class FutureInvestment {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

//Obtain Investment Amount a
System.out.println("Enter investment amount: ");
double a = input.nextDouble();

//Obtain Interest Rate r
System.out.println("Enter interest rate: ");
double r = input.nextDouble();

//Obtain number of Years y
System.out.println("Enter number of years: ");
double y = input.nextDouble();

//Compute Future Investment Amount f
double f = a * Math.pow(1 + r, (y * 12));

//Display result
System.out.println("Accumulated value is " + f);

}
}

OUTPUT IS...........................

Enter investment amount:
1000
Enter interest rate:
.0425
Enter number of years:
1
Accumulated value is 1647.8313602236476
• 09-11-2012, 05:08 AM
Junky
Re: Java Calculate Future Investment Amount
We have no idea what is wrong unless you tell us. Are you getting incorrect output? Then you should post what you expect the output should be. If it is wrong then most likely your equation is incorrect. Where did you get it? Are you sure you entered it into your code correctly?
• 09-11-2012, 05:11 AM
rjanisze
Re: Java Calculate Future Investment Amount
Expected Output is:
Enter investment amount: 1000
Enter annual interest rate: 4.25
Enter number of years: 1
Accumulated value is 1043.34

Actual OUTPUT IS...........................

Enter investment amount: 1000
Enter interest rate: .0425
Enter number of years: 1
Accumulated value is 1647.8313602236476
• 09-11-2012, 05:32 AM
java.java
Re: Java Calculate Future Investment Amount
There is a difference between 4.25 and .0425 , please correct your input data and validate the output. If the equation you are using is correct then you should get the same output.
• 09-11-2012, 05:44 AM
rjanisze
Re: Java Calculate Future Investment Amount
Enter investment amount:
1000
Enter interest rate:
4.25
Enter number of years:
1
Accumulated value is 4.384414858452464E11

The r must be input .01...etc
• 09-11-2012, 06:45 AM
rjanisze
How do I do a Hard Truncate the output in this Java code after the second decimal?

import java.util.Scanner;

public class FutureInvestment {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

//Obtain Investment Amount a
System.out.println("Enter investment amount: ");
double a = input.nextDouble();

//Obtain Monthly Interest Rate r
System.out.println("Enter interest rate: ");
double r = input.nextDouble();

//Obtain number of Years y
System.out.println("Enter number of years: ");
double y = input.nextDouble();

double f = a * Math.pow(1 + (r/12),(y *12));

//Display result
System.out.println("Accumulated value is " + f);

}
}

OUTPUT IS
Enter investment amount: 1000
Enter interest rate: .0325
Enter number of years: 1
Accumulated value is 1032.9885118105894

I want it to say 1032.98
• 09-11-2012, 07:10 AM
pbrockway2
Re: How do I do a Hard Truncate the output in this Java code after the second decimal
Have a read of the options discussed in Oracle's Tutorial: Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

Quote:

Accumulated value is 1032.9885118105894

I want it to say 1032.98
This sort of rounding where you simply truncate (== throw away insignificant digits) can be done using a RoundingMode.

Code:

```import java.math.RoundingMode; import java.text.DecimalFormat; public class Test {     public static void main(String[] args) {         DecimalFormat fmt = new DecimalFormat(".000");         fmt.setRoundingMode(RoundingMode.DOWN);             // prints 3.141         System.out.println(fmt.format(Math.PI));     } }```
In this context "DOWN" means "towards zero". Notice that pi is actually closer to 3.142 than it is to 3.141.
• 09-11-2012, 08:42 AM
DarrylBurke
Re: Java Calculate Future Investment Amount
Don't start a new thread for what is essentially a follow-on question, and moreover one which you already asked in the first thread. I've merged the two threads.