jeudi 29 octobre 2015

Missing Return Statement within While Loop w/no exits

Long time reader, first time poster. I appreciate how helpful this community has been to me before, and I hope I can find an answer to my question!

I have a java method that is "missing a return statement." I have looked into this heavily, and know that the problem is most likely that the compiler found a path within an 'if' function which doesn't offer a return statement. However, my while loop should be set up so that when a path within an 'if' function reaches an endpoint where it doesn't return anything, it ends up looping the entire method.

The overall goal of the method is for the user to type in a file that they want to output to, but if they choose one that exists and then choose not to overwrite it, the initial question asking for a new file name is reexecuted, until a new file is entered or an existing file is entered and the user agrees to overwrite it.

Here is my code, does anyone know how to fix it? Thank you very much

    public static PrintStream getOutputPrintStream(Scanner console){
    boolean done = false;
    while (done = false) {
        System.out.print("Enter output file: ");
        File outFile = new File(console.next());

        if (outFile.exists()) {
            System.out.print("The file " + outFile + " already exists, would you like to overwrite it? (y/n): ");
            String overwrite = console.next();
            overwrite = overwrite.toLowerCase();
            char decision = overwrite.charAt(0);
            if (decision == 'y') {
                    try {
                        PrintStream output = new PrintStream(outFile);
                        done = true;
                        return output;
                    } catch (FileNotFoundException e){
                        System.exit(1);
                    }
            }
        } else {
            try {
                PrintStream output = new PrintStream(outFile);
                done = true;
                return output;
            } catch (FileNotFoundException e){
                        System.exit(1);
                    }
        }


    } 

}

Aucun commentaire:

Enregistrer un commentaire