Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7521427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:14:00+00:00 2026-05-30T02:14:00+00:00

when I run the for loop towards the bottom of this code I get

  • 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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:14:01+00:00Added an answer on May 30, 2026 at 2:14 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run this code, About half-way through the concatenation loop, $xml becomes null
This is error in the log console that I get when I run my
I'm trying to run this for loop; for (int col= 0; grid[0].length; col++) However
I had a Java program that I run thousand times based on a loop
I've run into this while writing a Traveling Salesman program. For an inner loop,
Below is my innermost loop that's run several thousand times, with input sizes of
I have a view that receives new data from a secondary thread. Every time
I'm trying to create a thread that configures a run loop to run a
Per Apple’s Polling Versus Run-Loop Scheduling: [ hasSpace/BytesAvailable ] can mean that there is
Does it run in a loop polling the contents? (Highly unlikely and inefficient.) Or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.