1

I am trying to output a file scanner object from my method. This is a school assignment and I am specifically instructed to NOT throw any exceptions, but use try/catch instead. The assignment requires that the command line prompt the user for a file to scan. If the file does not exist, we are supposed to tell the user, then prompt them for a file again. If the file does exist, then the method returns a scanner object that scans the file.

My code works, but it is not clean. It involves 2 methods. This is my code so far:

public static Scanner getInputScanner (Scanner console) {
    File inputFile = null;
    Scanner input = null;
    try {
        inputFile = getFile(inputFile, console);
        input = new Scanner (inputFile);
        return input;
    } catch (FileNotFoundException e) {
        try {
            return input = new Scanner (getFile (inputFile, console));
        } catch (FileNotFoundException f) {
            System.out.println("An error has occured.");
            return input;
        }
    }

}

public static File getFile (File inputFile, Scanner console) {

    System.out.println("Enter input file: ");
    inputFile = new File (console.nextLine());

    while (!inputFile.exists()) {
        System.out.println("File does not exist.");
        System.out.print("Enter input file: ");
        inputFile = new File (console.nextLine());
    }
    return inputFile;
}

The problem with the code is that the output looks like this:

Enter input file:

File does not exist.

Enter input file:

It then is waiting for the user's input. I don't want the output to have the 2 lines of code before the last line though.

Can anybody explain why my code is outputting these 2 lines? Also, is there a simpler solution to getting an input file without throwing the FileNotFoundException?

Thanks!

M. Fleck
  • 13
  • 1
  • 4

3 Answers3

0

If I understand correctly, your program outputs these lines when you run it, no matter what, without you getting a chance to actually enter a filename.

Enter input file:
File does not exist.

And then the programs asks you again:

Enter input file:

And you don't want the first two lines above, right? This can happen for example if the Scanner console you received has an unread newline in it. You haven't posted that part of the code, so it's hard to tell, but this is a common gotcha with Scanner. Before calling getInputScanner, make sure the Scanner console is ready to use, with no unread garbage still buffered in it.

As for the second part of your question, yes this can be written simpler and better, for example:

public static Scanner getInputScanner(Scanner console) {
    try {
        File inputFile = getExistingFile(console);
        return new Scanner(inputFile);
    } catch (FileNotFoundException e) {
        throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
    }
}

public static File getExistingFile(Scanner console) {
    while (true) {
        System.out.println("Enter input file: ");
        File inputFile = new File(console.nextLine());

        if (inputFile.exists()) {
            return inputFile;
        }
        System.out.println("File does not exist.");
    }
}
janos
  • 120,954
  • 29
  • 226
  • 236
  • Thanks for the input janos. Yes, you are correct. My program executes the first 2 lines of code no matter what. – M. Fleck Oct 31 '15 at 18:16
  • Also, thank you for the code you responded with. It is definitely more concise. I used it just now, but unfortunately, I get the same output. I think you may be correct about the scanner having some unread tokens buffered in it. – M. Fleck Oct 31 '15 at 18:18
  • Before calling this method, calling `nextLine` on the scanner might be a simple fix – janos Oct 31 '15 at 18:22
  • Janos, this was the problem. All I did was add a console.nextLine() before the "Enter input file: " prompt and it all looks good. Thanks so much for the tip. I spent hours trying to figure this out. – M. Fleck Oct 31 '15 at 18:27
0

It execute below line as soon the getFile() being called.

System.out.print("Enter input file: ");

Since no file exist, the below lines keeps on executing :

while (!inputFile.exists()) {

    System.out.println ("File does not exist.");
    System.out.print("Enter input file: ");

You can use throws() instead of try/catch, then caller will take care of exception.

KP_JavaDev
  • 222
  • 2
  • 4
  • 11
0

Had to consume whatever junk was being carried over from the scanner by inserting a Scanner.nextLine() before getting user input. Final code looks like this:

public static Scanner getInputScanner(Scanner console) {
    try {
        File inputFile = getExistingFile(console);
        return new Scanner(inputFile);
    } catch (FileNotFoundException e) {
        throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
    }
}

public static File getExistingFile(Scanner console) {
    while (true) {
        console.nextLine();
        System.out.println("Enter input file: ");
        File inputFile = new File(console.nextLine());

        if (inputFile.exists()) {
            return inputFile;
        }
        System.out.println("File does not exist.");
    }
}
M. Fleck
  • 13
  • 1
  • 4