Search code examples
javafile-iofilereaderprintwriterstringtokenizer

"File missing! "Error for USACO Java program


I get an error saying my file is missing as standard issue. I've closed out my file which flushes the FileReader, making the issue even more ambiguous. I attempted different output methods with BufferedWriter, FileOutputStream, and DataOutputStream.The output is correct as it matches up with all the given test cases. Any input is helpful. Thank you.

public static void main(String[] args) throws Exception {

    BufferedReader br = new BufferedReader(new FileReader("mixmilk.in"));   
    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("mixmilk.out")));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int c1 = Integer.parseInt(st.nextToken());
    int m1 = Integer.parseInt(st.nextToken());
    st = new StringTokenizer(br.readLine());
    int c2 = Integer.parseInt(st.nextToken());
    int m2 = Integer.parseInt(st.nextToken());
    st = new StringTokenizer(br.readLine());
    int c3 = Integer.parseInt(st.nextToken());
    int m3 = Integer.parseInt(st.nextToken());

    if (m1 + m2 <= c2) {
        m2 = m1 + m2;
        m1 = 0;
    }
    else {
        while (m2 < c2) {
            m2++;
            m1--;
        }
    }

    if (m2 + m3 <= c3) {
        m3 = m2 + m3;
        m2 = 0;
    }
    else {
        while (m3 < c3) {
            m3++;
            m2--;
        }
    }

    if (m3 + m1 <= c1) {
        m1 = m3 + m1;
        m3 = 0;
    }
    else {
        while (m1 < c1) {
            m1++;
            m3--;
        }
    }

    if (m1 + m2 <= c2) {
        m2 = m1 + m2;   
        m1 = 0;
    }
    else {
        while (m2 < c2) {
            m2++;
            m1--;
        }
    }

    System.out.println(m1);
    System.out.println(m2);
    System.out.println(m3); //what im printing to the file 
    pw.println(m1);
    pw.println(m2);
    pw.println(m3);
    pw.println("\n"); // recommended due to new UNIX environment for grading servers
    pw.close();
}

}


Solution

  • You haven't provided enough information (stacktrace, error message, sample input, etc) so I am going to have to resort to a significant amount of guesswork (!!)

    The "missing file" error is going to be caused by either

    new FileReader("mixmilk.in")
    

    or

    new FileWriter("mixmilk.out")
    

    Assumption #1: I am assuming that you didn't provide a stacktrace because there wasn't one.

    Assumption #2: Your application itself will not output an error message that says "File missing!", so I am assuming it must be coming from the test harness; i.e. code that is running your code and testing the output

    Assumption #3: The test harness is going to supply its own input file, so it is unlikely to complain that the file is missing. Therefore, it must be complaining that it cannot find the file that your application is supposed to be writing.

    This means that new FileWriter("mixmilk.out") is wrong. Here are some possibilities:

    • You have used the wrong filename; e.g. maybe the file extension is incorrect, or the filename should be "MixMilk.out" or something. (Note that Linux / UNIX filenames are case sensitive.)

    • Your application will attempt to write a file into the current directory. Perhaps that is incorrect. Perhaps it should write it somewhere else; e.g. a temporary directory.

    • Perhaps your application is not supposed to read and write files at all. Perhaps it is supposed to read and write standard input/output? Perhaps the filenames / pathnames are supposed to be command line arguments?

    All of these things boil down to you not fully understanding the requirements for the assignment. (I can only guess ... because you have not included the requirements!)


    The other possibility is that some of the information in your Question is inaccurate. For example, I have assumed that 1) the error message is "File missing!" like you told us, and 2) there is no stacktrace ... because you didn't respond when someone asked you to add the stacktrace!

    A final possibility is that the problem is in the test harness (i.e. a bug) or that the requirements are ambiguous. Talk to other students doing the assignment.