File input and file output
Could someone help me figure out why my program won't input the data back onto the screen after it output it onto a text file
Thanks
Code :
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;
import java.util.Random;
public class BottleCapPrize
{
public static void main(String[]args) throws IOException
{
PrintWriter outFile = new PrintWriter(new File("MonteCarlo.txt"));
Scanner inFile = new Scanner(new File("MonteCarlo.txt"));
Random randNum = new Random();
int bottleCapNum = 0;
int wins = 0;
int bottleCapInput = 0;
int token;
Scanner userInput = new Scanner (System.in);
//
System.out.print("Please enter the number of trials: ");
int uInput = userInput.nextInt();
for (int counter = 1; counter <= uInput; counter++) {
int randNum1 = 0;
while (randNum1 != 1) {
randNum1 = randNum.nextInt(5);
System.out.println("randNum1 = " + randNum1);
bottleCapNum++;
}
wins++;
bottleCapNum++;
System.out.println("Counter = " + counter + " bottleCapNum = "
+ bottleCapNum + " wins = " + wins);
outFile.println(bottleCapNum);
}
outFile.close( );
while(inFile.hasNextInt());
{ token = inFile.nextInt();
System.out.println(token);}
inFile.close();
}
}
Re: File input and file output
What does the code print out when it executes?
Does the program end normally?
Add some println statements so you can see where the code is executing and find the last statement that it executed.
Re: File input and file output
I'm trying to make the program output the number into the file and then get it back from that file.
Re: File input and file output
What does the code print out when it executes?
Does the program end normally?
Add some println statements so you can see where the code is executing and find the last statement that it executed.
Re: File input and file output
It would output the result to the text file but it seem the program is not ending. It get stuck I'm guessing at the line to read the text file and print them on the screen.
Re: File input and file output
Try adding some println statements to see where the code is hanging/stuck. Sometime you need them every other statement so you can tell which statement the program is hanging on. If one println doesn't print, then you know that the program is hung on the statement just in front of it.
Re: File input and file output
The code is stoping after it print "outFile.println(bottleCapNum);" So the while loop to read the file is where the program stuck at.
Re: File input and file output
What statement is the one before the last println statement that did not print?
EDIT: corrected above
When it did not print, that means the statement before it is hung.
Re: File input and file output
while(inFile.hasNextInt());
{ token = inFile.nextInt();
Re: File input and file output
That's two statements. Which one is it hanging on? Put a println between them.
Re: File input and file output
This is what I put
while(inFile.hasNextInt());
{System.out.println(inFile);
token = inFile.nextInt();
System.out.println(token);}
But it did not print the inFile so it hanging at the start of the while loop.
Re: File input and file output
Quote:
But it did not print the inFile so it hanging at the start of the while loop.
Take a close look at the while statement and make sure there isn't a statement ending ; before the { that surrounds the code that is supposed to be in the loop.
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: File input and file output
Oh wow... I miss such a little detail. Thank you so much. Can't believe I been trying to figure out what wrong and it end up being that ; T~T