# Static Method Help

• October 9th, 2011, 04:35 PM
mysticCHEEZE
Static Method Help
I'm trying to use a static method to find out how many years it takes to double an arbitrary amount of money so I used a dummy amount of money. The problem is, the code compiles but the code does not run properly. What am I doing wrong?
Code :

class YearsToDouble
{
public static void main ( String [] args ) throws Exception
{
System.out.println("rate(percent)   years to double");
for ( int percent : new int[] {1,2,3,4,6,9,12,24} ) line(percent);
}
public static void line ( int percent )
{
System.out.printf("%7d%17d\n",percent,yearsToDouble(percent));
}
public static int yearsToDouble ( int percent )
{
double balance = 1.00;
int years = 0;
while ( balance < 2.00)
{
balance += ((percent/100)*balance);
years++;
}
return years;
}
}

• October 9th, 2011, 05:15 PM
newbie
Re: Static Method Help
Code java:

while ( balance < 2.00)
{
balance += ((percent/100)*balance);
years++;
}

That's an infinite loop.
1 / 100, 24 / 100, calculated as Integers equate to 0.

So you're always saying, 0 * balance, which of course equals zero, hence infinite loop :)
• October 9th, 2011, 06:03 PM
mysticCHEEZE
Re: Static Method Help
Now I get it Thanks!