I am having a problem with a program I wrote.. For the life of me I can't figure out what's wrong with the program.. the program should calculate a random value and then outputs an intrest rate and present value.. The problem is that I am getting zeros instead of real numbers.

``````import java.lang.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;

public class CompoundFormulaLab {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
double presentValue= 0;
double futureValue = 1000000;
//int x = 100 + (int)(Math.random()*200);
Random randomizer = new Random ();
double calcInterest =(double) (randomizer.nextInt(10));
double yearsInFuture = 20;
//double dollarAmount=0;
double interestRate=0;
double annualIntrestRate=0;

annualIntrestRate = calcInterest  / 100;

presentValue = futureValue/(Math.pow(1+(annualIntrestRate), futureValue));

//DecimalFormat money = new DecimalFormat("\$0.00");

DecimalFormat dollarAmount = new DecimalFormat("\$#.##");

interestRate = presentValue * annualIntrestRate;

NumberFormat interestRatePrint = NumberFormat.getNumberInstance();
interestRatePrint.setMinimumFractionDigits(3);

//System.out.print(dollarAmount.format(presentValue));

System.out.print("The amount of dollars is " +dollarAmount.format(presentValue) +" and the interest is " +interestRatePrint.format(interestRate) +"%");

}

}
``````

This is the whole program.. I appreciate your help

## All 5 Replies

Instead of calculating the present value of a million dollars twenty years in the future, you are calculating the present value of a million dollars a million years in the future, and that is much less than one cent. The result of `Math.pow(1+(annualIntrestRate), futureValue)` is `Double.POSITIVE_INFINITY` so it's mostly by luck that you get the right answer in the end, but on the other hand it's hard for me to imagine money a million years in the future being worth anything now. I certainly wouldn't pay anything for it.

A million years of compound interest is just too many years for calculating in the normal way. But you can do it with `java.math.BigDecimal` as long as you don't want exact precision. I tried it with exact precision and the `pow` method failed to return within a practical length of time.

I got all zeros.. It's funny because my instructor requirements are not clear at all.. He always does that and get me thinking of things that are not worth it.. Here is my updated code..

``````import java.lang.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;

public class CompoundFormulaLab {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
double presentValue= 0;
double futureValue = 1000000;
//int x = 100 + (int)(Math.random()*200);
Random randomizer = new Random ();
double calcInterest =(double) (randomizer.nextInt(10));
double yearsInFuture = 20;
//double dollarAmount=0;
double interestRate=0;
double annualIntrestRate=0;

annualIntrestRate = calcInterest  / 100;

presentValue = futureValue/(Math.pow(1+(annualIntrestRate), futureValue));

//DecimalFormat money = new DecimalFormat("\$0.00");

DecimalFormat dollarAmount = new DecimalFormat("\$#.##");

interestRate = presentValue * annualIntrestRate;

NumberFormat interestRatePrint = NumberFormat.getNumberInstance();
interestRatePrint.setMinimumFractionDigits(3);

//System.out.print(dollarAmount.format(presentValue));

System.out.print("The amount of dollars is " +dollarAmount.format(presentValue) +" and the interest is " +interestRatePrint.format(interestRate) +"%");

}

}
``````

It is still not outputting the right output.. I believe my logic is right but I am missing something.

I believe my logic is right but I am missing something.

You are missing the extra 999980 years in your calculation. Perhaps I wasn't clear in my previous post, but to me, `double yearsInFuture = 20;` means that you are trying to calculate the present value of money 20 years in the future, but that's not what you are calculating. Your zeros are the accurate calculation of the present value of money one million years in the future.

In other words, if someone offered you a postdated cheque that couldn't be used for a million years, what would you pay for it? If you think about that, you will realize that your program is printing the correct answer.

Huh? He gave us the formula.. And he said we have to use it.. I can't modifiy it because you say "it's wrong", and no I am calculating the 20 not the million as years, look at the formula again.

presentValue = futureValue/(Math.pow(1+(annualIntrestRate), futureValue));

Just re-read that - where are the years???

commented: Whoops my apologies.. I guess I rushed through it.. But I am getting a huge number as a percentage value. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.