2
import java.util.*;

public class Investment 
{

    public static int years=0;
    public static void main(String[] args) 
    {
        Scanner kbd = new Scanner(System.in);
        System.out.println("Please enter an integer representing the whole dollar value of your initial investment: $");
        int startBal=kbd.nextInt();
        System.out.println("Please enter the whole number percentage, excluding the percent sign, of the interest rate on the investment.");
        int interestRate=kbd.nextInt()/100;
        int endBal=startBal*2;
        int currentBal=startBal;
        while (endBal>=currentBal)
        {
            currentBal=currentBal*interestRate+currentBal;
            years++;
        }
        System.out.println("It will take "+years+" years to double your investment.");
    }
}

The output I'm seeing is:

Please enter an integer representing the whole dollar value of your initial investment: $
10000
Please enter the whole number percentage, excluding the percent sign, of the interest rate on the investment.
5

The "10000" and "5" are my input. The program should be printing my final statement with the answer "15," but instead doesn't do anything, and doesn't terminate.

Kara
  • 6,115
  • 16
  • 50
  • 57
Squidicide
  • 57
  • 4

5 Answers5

3
int interestRate=kbd.nextInt()/100;

Since interestRate is an int, it gets rounded down to zero.

So this line:

currentBal=currentBal*interestRate+currentBal;

resolves to:

=> currentBal=currentBal*0+currentBal;
=> currentBal=0+currentBal;
=> currentBal=currentBal;

So the value never increases, so will never reach double its initial value, so infinite loop.

You would have to replace the line with:

double interestRate=kbd.nextInt()/100.0;
AntonH
  • 6,359
  • 2
  • 30
  • 40
  • 2
    @Squidicide No problem :) Instead of editing title as "Solved - ...", accept an answer, and it will appear different on the Questions page. – AntonH Oct 16 '14 at 18:03
1

Your interestRate is declared as an int so it will be set to 0.

You should declare all values as doubles, since we-re talking about money we want to be precise :)

VAndrei
  • 5,420
  • 18
  • 43
1

You have an int interest rate, which when divided by 100, becomes 0. Make it a double and use the double literal 100.0 to divide, to force floating-point division.

double interestRate=kbd.nextInt()/100.0;

The currentVal variable will need to be a double also.

My output with the changes:

Please enter an integer representing the whole dollar value of your initial investment: $
10000
Please enter the whole number percentage, excluding the percent sign, of the interest rate on the investment.
5
It will take 15 years to double your investment.

This will let the program finish. But normally it's not a good idea to use a double to store money.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

Your doing integer division 5/100 which = 0 remainder 5. So in your calculation you are multiplying by 0 so your current balance will always be 0 and never reach end balance. Try using a double for your interest rate rather than an integer.

brso05
  • 13,142
  • 2
  • 21
  • 40
0
while (endBal>=currentBal)
{
    currentBal=currentBal*interestRate+currentBal;
    years++;
}

of course it never terminate, you are only incrementing years but the variables on the while are endBal and currentBal

tviana
  • 407
  • 4
  • 20
  • 1
    However, the currentBal is being continuously modified while iterating, Isn't it ? and infinite looping is because of the user dividing 5/100, which is always going to be 0, and hence the interestRate and as a consequence the currentBal also. – Amitesh Rai Oct 16 '14 at 17:40