0

I'm currently having an issue where a 'while' loop is not executing. I set the loop condition to be true if an input text file has a next line. However, when I executed my program, the loop did not run. I confirmed this by adding a 'System.out.println(text)', and as I suspected, there was no text that resulted.

What issue is causing the loop to not execute?

import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.Random;
import java.util.Scanner;

public class BottleCapPrize
{
public static void main (String[] args) throws IOException
{
    Scanner in = new Scanner(System.in);
    PrintWriter outFile = new PrintWriter(new File("guess.txt"));
    Scanner inFile = new Scanner(new File("guess.txt"));
    Random rand = new Random();

    System.out.print("Enter number of trials: ");
    int trials = in.nextInt();

    int guess = 0;
    int totalGuess = 0;

    for (int trial = 0; trial < trials; ++trial)
    {
        guess = 0;

        int winCap = rand.nextInt(5) + 1;
        int guessCap = rand.nextInt(5) + 1;

        ++guess;
        while (guessCap != winCap)
        {
            guessCap = rand.nextInt(5) + 1;
            ++guess;
        }

        outFile.println(guess);
    }

    while (inFile.hasNextLine())
    {
        String number = inFile.nextLine();
        guess = Integer.parseInt(number);
        totalGuess += guess;

        System.out.println("This should print if while loop conditions set to true.");
    }

    double average = (double)totalGuess / trials;

    System.out.println(totalGuess + " " + guess);
    System.out.println("On average, it took " + average + " bottles to win.");
    System.out.println();

    inFile.close();
    outFile.close();

}
}
4
  • Did you try stepping through it in the debugger? Commented Sep 20, 2015 at 0:50
  • Are you sure that your .txt file contains text? Commented Sep 20, 2015 at 0:53
  • Yes, I checked the 'guess.txt', and there are numbers. Could it be that the new File is overriding the .txt? Commented Sep 20, 2015 at 0:54
  • I removed the answer from the question text. If you accepted other user's answer that's enough. If you figured out how to solve your problem and feel that already posted answers are incomplete, post your own answer instead of editing the question. Commented Sep 20, 2015 at 5:54

2 Answers 2

3

Make sure to flush the contents to guess.txt file after you use outFile.println(guess) by doing outFile.flush().

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

2 Comments

Thanks, this worked. I read outFile.flush() automatically writes to the .txt file; but what happens usually without it?
Without the outFile.flush(), the writes are written to a temporary buffer, and not to the actual file. With the flush() method, the temporary buffer is flushed to the actual file.
0

The first step is to verify that your outer loop, the for loop is actually running as well. if trials happens to be 0 then your for loop will never run either, and you will never reach the while loop itself.

1 Comment

I believe the for loop is working, as when I execute the program, there are numbers generated in the guess.txt file.

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.