# My program always gives me an output of 0.0, what's wrong with it?

• December 30th, 2013, 10:00 PM
thepiranha
My program always gives me an output of 0.0, what's wrong with it?
*Edit: I fixed it thanks to aprabhat! Thanks for your help!*

I'm at the beginner level when it comes to Java, so I don't really know much about programming, but I'm taking a class on Java and we were asked to write a program that prompts the user for three different values (P,i, and n as you will see in my code) and use those values to calculate interest on a certain amount of money (P) that the user is prompted for. So I wrote it all and it will compile and run, but for some reason I always get an output of zero. I have tried using both double and float variables instead of integer variables because I know that integer variables always round, but it doesn't work. Also, when I use float variables my program won't compile because the power method requires a double variable because there is "a possible loss of precision" so I used double variables in the code in this post. Thanks ahead of time for the help. Well anyways, here's my code:

public class Unit7Assignment_2
{
public static void main( String [] args )
{
int P = 0;
double i = 0;
int n = 0;
double S = interest( P, i, n );

P = Input.getInt( "Please enter an integer value for the amount of money deposited at the beginning of the year" );
i = Input.getDouble( "Please input a double value for the annual interest rate" );
n = Input.getInt( "Please enter an integer value for the amount of time in years that the money deposited has accrued the interest" );

if ( n == 1 )
{
System.out.println();
System.out.println( "The amount of money in the account after 1 year is \$" + S );
System.out.println();
}
else
{
System.out.println();
System.out.println( "The amount of money in the account after " + n + " years is \$" + S );
System.out.println();
}
}

public static double interest( double P, double i, double n )
{
double j = 1 + i;
double k = Math.pow( j, n );
double m = k * P;
return m;
}
}
• December 30th, 2013, 10:42 PM
helloworld922
Re: My program always gives me an output of 0.0, what's wrong with it?
Check the order of your commands. Java executes top-down in a block.
• December 30th, 2013, 11:03 PM
thepiranha
Re: My program always gives me an output of 0.0, what's wrong with it?
Which commands? The equations to calculate my return value?
• December 30th, 2013, 11:29 PM
aprabhat
Re: My program always gives me an output of 0.0, what's wrong with it?
```double S = interest( P, i, n ); P = Input.getInt( "Please enter an integer value for the amount of money deposited at the beginning of the year" ); i = Input.getDouble( "Please input a double value for the annual interest rate" ); n = Input.getInt( "Please enter an integer value for the amount of time in years that the money deposited has accrued the interest" );```