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.

2
  • 2
    This is a good question. Code is there. Input, expected output, and actual output are all there. Commented Oct 16, 2014 at 17:36
  • 2
    Please don't edit in "solved" to your title. Here in SO, you can "accept" exactly one answer that solved your problem by clicking the checkmark next to that answer. Commented Oct 16, 2014 at 17:56

5 Answers 5

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;
Sign up to request clarification or add additional context in comments.

1 Comment

@Squidicide No problem :) Instead of editing title as "Solved - ...", accept an answer, and it will appear different on the Questions page.
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 :)

Comments

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.

Comments

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.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.