alright, i think this may be the way to do what you were describing, PrintWriter outFile = new PrintWriter (new File("bottleCap.txt"));
while(counter <= 20)
{
randomNumber = ((int)(0+ Math.random()* 5));
outFile.println(winCounter);
if (randomNumber == 1)
{
winCounter++;
}
}
outFile.close ( );
it seems like that would keep it from printing out all those zeros, but now when i go to open the txt to look at the output notepad goes non-responding.
You have an infinite loop here. counter never gets adjusted.
You need to step back and look at your program and do a redesign. For 20 trials, you will generate far more than 20 random numbers. You don't know how many numbers you'll need to generate, but the number of trials will be known (20, later to be 1000), so stick that in a for-loop.
Within each trial, you'll generate a random number and from that, decide whether you have a "winner". You've decided to have the range be 0 to 4 and have a winner declared if that number is 1. That's fine. When 1 shows up, that's the end of the trial, correct? Hence that needs to be somewhere in a while loop. For brevity's sake, let's say you have three trials. Let's say these are your random numbers. Note that you do not know in advance how many random numbers you'll need to produce. Here's one "run".
Lines 1 through 2 are trial 1. Lines 3 through 7 are trial 2. Lines 8 through 14 are trial 3. Your output file will be this:
It took 2 tries for trial 1, 5 for trial 2, 7 for trial 3.
We're talking about a nested loop. How many trials? 3. That's an exact number known ahead of time, so we're talking about a for-loop. The inner loop is the trial itself. Do we know how many times that loop will be gone through in advance? No. While loop or Do-While loop. Is it guaranteed to occur at least once? Yes? Do-while loop (you could have a plain old while loop if you prefer). How do we know when the inner loop is done? When the random number is 1. Keep that in mind when thinking of the loop condition you need to write.
How many numbers are we outputting to the file? 3. This answer should help you place where to put the file output statement.