Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Am I generating an infinite loop?

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Am I generating an infinite loop?

    EDIT: Fixed the code a little so the loop part isn't an issue, issue now is that the average isn't generating
    The purpose of this program is to run an experiment of multiple trials seeing how many squirrels you see before seeing a fox squirrel.
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.File;
    import java.util.Scanner;
    import java.util.Random;
    class Main {
      public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        File fileName =  new File("squirrel.txt");
        Scanner inFile = new Scanner(fileName);
        PrintWriter outFile =  new PrintWriter(new File("squirrel.txt"));
        Random ran = new Random();
        System.out.println("Welcome to the Fox Squirrel Simulator");
        System.out.println();
        System.out.println("How many trials should be simulated?");
        System.out.println("Please enter a number greater than 1000: ");
        int trialNum = input.nextInt();
        if (trialNum < 1000) {
          System.out.println("\tError, please try again.");
          System.out.println("How many trials should be simulated?");
          System.out.println("Please enter a number greater than 1000: "); 
        trialNum = input.nextInt();
        } 
        int initialTrialNum = trialNum;
        System.out.println("trialNum is " + trialNum);
        System.out.println();
        System.out.println("Simulating trials, please wait...");
        int squirrelsSeen;
        int foxSquirrel;
        int trialsDone;
        for (trialsDone = 1; trialsDone <= trialNum; trialsDone++) {
          squirrelsSeen = 0;
          for(foxSquirrel = 0; !(foxSquirrel == 10); squirrelsSeen++) {
            foxSquirrel = ran.nextInt(11);
          }
          outFile.println(squirrelsSeen);
          System.out.println(squirrelsSeen);
        }
        String token = "";
        int sum = 0;
        int fileInt = 0;
        while(inFile.hasNext()) {
          token = inFile.next();
          fileInt = Integer.parseInt(token);
          sum += fileInt;
        }
        double average = (double)sum/(double)initialTrialNum;
        System.out.println("The results! The average number of squirrels \n saw until a fox squirrel was spotted is: " + average);
      }
    }
    I am getting no output from the average, it is giving me a value of 0.0. I use repl.it to compile, does that affect it from writing into the text file? I have the text file created in the same folder as the repl.it.
    Last edited by bamboozled40; September 10th, 2019 at 08:40 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Am I generating an infinite loop?

    I assume there is something wrong with the for loops that is causing it to get stuck in an infinite loop.
    Add some debugging print statements to the loops that print out the value of the variables that controls the looping. The print out should show you what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Am I generating an infinite loop?

    it is giving me a value of 0.0
    What variable contains that value of 0? Where is the code that should change the value in that variable?
    Is the code that is supposed to change the variable's value being executed?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why is this in an infinite loop?
    By Ludus in forum Loops & Control Statements
    Replies: 8
    Last Post: November 19th, 2013, 06:40 AM
  2. Why do I get an infinite loop?
    By Skynet928 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 22nd, 2013, 08:29 PM
  3. Infinite loop?
    By jackfletcher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2012, 01:06 PM
  4. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM