0

when I run the for loop towards the bottom of this code I get an infinite loop that outputs the output in the if statement every single time with i being incremented each time and doesn't stop. I'm pretty sure it's a small problem that I just can't put my finger on...

    import java.util.Scanner;

public class HW2johnson_pp1 {

    public static void main(String args[]) {

        Scanner keyboard = new Scanner(System.in);

        System.out.printf("Please enter the species with the higher" + 
                          " population first\n");
        System.out.printf("Enter the name of your first species ");
        String Species1 = keyboard.nextLine();
        System.out.printf("Enter the species' population: ");
        int Pop1 = keyboard.nextInt();
        System.out.printf("Enter the species' growth rate: ");
        int Growth1 = keyboard.nextInt();

        System.out.printf("Enter the name of your second species: ");
        String Species2 = keyboard.nextLine();
        System.out.printf("Enter the species' population: ");
        int Pop2 = keyboard.nextInt();
        System.out.printf("Enter the species' growth rate: ");
        int Growth2 = keyboard.nextInt();

        if (Pop2 > Pop1) {
            System.out.printf("The first population must be higher. \n");
            System.exit(0);
        }


        Species input1 = new Species();
        input1.name = Species1;
        input1.population = Pop1;
        input1.growthRate = Growth1;

        Species input2 = new Species();
        input2.name = Species2;
        input2.population = Pop2;
        input2.growthRate = Growth2;

        if ((input1.predictPopulation(1) - input2.predictPopulation(1)) <= 
            (input1.predictPopulation(2) - input2.predictPopulation(2))){

            System.out.printf(Species2 + " will never out-populate " + 
                              Species1 + "\n");
        }
        else {

            for (int i = 0; input2.predictPopulation(i) <= 
                            input1.predictPopulation(i); i++) {

                if (input2.predictPopulation(i) == input1.predictPopulation(i)) {
                    System.out.printf(" will out-populate \n");
                }
            }
        }
    }
}

public int predictPopulation(int years)
        {
            int result = 0;
            double populationAmount = population;
            int count = years;
            while ((count > 0) && (populationAmount > 0))
            {
                populationAmount = (populationAmount +
                              (growthRate / 100) * populationAmount);
                count--;
            }
            if (populationAmount > 0)
                result = (int)populationAmount;

            return result;
        }
4
  • 1
    Impossible question to answer if you don't show what predictPopulation() does. Commented Feb 19, 2012 at 23:27
  • This is impossible to answer, as you haven't shown the definition of predictPopulation(). All that can be said is that clearly input2.predictPopulation(i) never exceeds input1.predictPopulation(i). Commented Feb 19, 2012 at 23:28
  • Sorry, guys I got sidetracked while posting thise. predictPopulation() is up there now Commented Feb 19, 2012 at 23:33
  • 2
    I suggest you step through your program in the debugger to find out what's going on. Commented Feb 19, 2012 at 23:34

2 Answers 2

3

This:

        if (Pop2 > Pop1) {
            Species2 = Species1;
            Pop2 = Pop1;
            Growth1 = Growth2;
        }

does not make sense. Apparently what you intend is to "swap" the values for the two populations, but instead, you just cause the two populations to be identical, so that for any value of years, predictPopulation(years) will be the same for both objects.


Edited to add: This test:

        if ((input1.predictPopulation(1) - input2.predictPopulation(1)) < 
            (input1.predictPopulation(2) - input2.predictPopulation(2)))

is not a valid way to see whether species 2 will eventually overtake species 1. If species 2 has a greater proportional growth rate (growthRate), then it will eventually overtake species 1, even if during the first few years, it has a lower absolute growth rate. (This only tangentially relates to your infinite loop — Dampsquid points out that you wouldn't have had the infinite loop if you'd had <= instead of < — but it's a problem you need to fix.)

Sign up to request clarification or add additional context in comments.

11 Comments

Also if ((input1.predictPopulation(1) - input2.predictPopulation(1)) < (input1.predictPopulation(2) - input2.predictPopulation(2))) needs a <= to catch identical growth popluations
@Dampsquid: Good point, thanks. I've added that to the answer.
@Dampsquid: Actually, on second thought . . . it's more complicated than that. The current test is wrong in general, not just for using a < rather than a <=. If input1 starts out with a very tiny population, then it might start out seeming to grow more slowly (since growth is proportional to population), but still end up overtaking input2 (since the one with the greater growthRate will always end up bigger in the long term).
Are you sure? Because the population increases more and more each year.
@ruakh: hmm very true, this homework needs much more thinking about, glad its not mine (finished homework decades ago thankfully)
|
1

Your issue is here

    for (int i = 0; input2.predictPopulation(i) <= 
                    input1.predictPopulation(i); i++) {

The population of input1 is never becoming less then input2 this has to do with one of the following:

  1. input1 is getting a parameter value that would not allow it's "population" to become less then input2 (check ranges of inputs to make sure this can occur, maybe something like making sure that input2's growth rate is > input1)

  2. There is something wrong with the class itself that is forcing input1 to always be equal to or greater then input2.

EDIT: since you have posted the code for predictPopulation it looks like it is number one. Make sure that your growth rate for input2 is always greater then input1.

Comments

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.