# Thread: Java Calculate Future Investment Amount

1. Member
Join Date
Sep 2012
Posts
4
Rep Power
0

## 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

2. ## 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?

3. Member
Join Date
Sep 2012
Posts
4
Rep Power
0

## 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

4. Member
Join Date
Jul 2012
Posts
9
Rep Power
0

## 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.

5. Member
Join Date
Sep 2012
Posts
4
Rep Power
0

## 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

6. Member
Join Date
Sep 2012
Posts
4
Rep Power
0

## 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

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19

## 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)

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.

Java 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.

8. ## 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.

Please go through the following:
Forum Rules
http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•