简体   繁体   中英

Why doesn't my program read my file that was created using the same program?

I'm trying to create a program that makes a file that randomly generates numbers and I want the program to read those numbers off of the file and analyze it. If the randomly generated number doesn't equal 0, the program should keep generating numbers, but if it does equal 0, then the program will stop. However, it seems that my program is not reading those numbers.

I tried putting the outFile.close(); and inFile.close(); in a couple different places to see if that would fix anything, but it seems that didn't work out. I tried tracing my code with pen and paper, but I couldn't find anything wrong. Perhaps it could be my placement of outFile.close(); and inFile.close(); , but I couldn't find anything wrong with it.

import java.util.*;
import java.io.*;

public class squirrel {

public static void main(String[] args) throws IOException{
    Scanner in = new Scanner(System.in);
    PrintWriter outFile = new PrintWriter(new File("squirrel.txt"));
    Scanner inFile =  new Scanner(new File("squirrel.txt"));
    Random rand = new Random();
    int squirrelNum;
    int foxSquirrel = 0;
    int squirrelsSeen = 0;
    int trials = 0;

    System.out.println("Welcome to the Fox Squirrel Simulator\n");
    System.out.println("How many trials should be simulated?");
    System.out.println("Enter a value greater than 1000: ");
    trials = in.nextInt();

    while(trials <= 1000)
    {
        System.out.println("Please try again. Enter a value greater than 1000: ");
        trials = in.nextInt();
    }

    for(int i = 0; i <= trials; i ++)
    {
        squirrelNum = rand.nextInt(10);
        outFile.println(squirrelNum);

        while(inFile.hasNextInt())
        {
            int token = inFile.nextInt();

            while(token != 0)
            {

                squirrelsSeen ++;
            }

            if(token == 0)
            {
                foxSquirrel ++;
                squirrelsSeen ++;
            }
            outFile.close();
        }


        System.out.println("squirrelsSeen: " + squirrelsSeen);
        System.out.println("foxSquirrel: " + foxSquirrel);
    }


    inFile.close();
    System.out.println("\nsimulating trials now... one moment please...\n");
    System.out.println("The results!");
    System.out.println("The average number of squirrels until spotting a Fox Squirrel at the city park is: " + (((double)foxSquirrel / squirrelsSeen) * 100));



 }

}

And here is how it is done using Scanner.

File file = new File("squirrel.txt");

    try {

        Scanner sc = new Scanner(file);

        while (sc.hasNextLine()) {
            int i = sc.nextInt();
            System.out.println(i);
        }
        sc.close();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }

And it is like you said. You put the .close() inside your while loop in your code. Try putting it outside.

new PrintWriter(new File("squirrel.txt")) immediately erases the file. From the documentation :

If the file exists then it will be truncated to zero size; otherwise, a new file will be created.

It is not possible to read and write the same file simultaneously. (Actually there are some cases were it's possible, but they don't apply here.)

Do not create your PrintWriter until you have finished reading from the file and have called inFile.close() . Or, write to a different file, then when you're done, rename it to match the original file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM