0

I have a java class where a user provides a file path and if the path doesn't exist I ask them to try again. My professor says we should use an exception to handle this.

Here is a snippet of how I'm currently doing it:

public class SalesUtil {

   public static void processSales() {

      Scanner keyboard = new Scanner(System.in);

      System.out.println("Enter sales file name: ");
      String salesFile = keyboard.nextLine();

      try {

         Scanner scanFile = new Scanner(new File(salesFile));

         //do stuff
         }
      } catch(FileNotFoundException fnfe) {

         System.out.println("Invalid file name supplied, please try again.");
         processSales();
      }
   }
}

Well in the do stuff section, I'm calculating values and printing data to the console. If I enter the correct file name correctly on the first try all the data is correct. If it is incorrect one or more times the data is not correct.

I imagine this is because of adding function calls on top of my initial stack and never 'getting out' of the initial stack while supplying subsequent stack calls until the correct file is supplied?

I'm still new to java and would appreciate some tips in understanding how to solve this using an exception.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
nullByteMe
  • 6,141
  • 13
  • 62
  • 99
  • This question is too broad to answer generally, but you're right that a recursive re-invocation is a bad idea. Instead, put your `scanner` variable in the method's scope, set it inside your `try` block, and set up a `(while scanner == null)` loop to contain the prompt and file-open attempt. – chrylis -cautiouslyoptimistic- Feb 26 '14 at 05:06

2 Answers2

1

use the file.exist() method to check, if that what you want to do is to make sure it exist then this is the codes:

File sfile = new File(salesFile);
if (sfile.exists()) {
    // ok, file exist do something.
   ...
}

On the other hand, when you say "invalid file" could be anything, if it is bad filename, then it is another animal (well, different exeception)...

To use try/catch for a readonly file then:

try {
   FileInputStream sfile = new FileInputStream(salesFile);
   ... 
} catch (FileNotFoundException e) {
   System.out.println(e.getMessage());
}
TA Nguyen
  • 453
  • 1
  • 3
  • 8
  • 1
    `My professor says we should use an exception to handle this.` – Michael Yaworski Feb 26 '14 at 05:11
  • Ok, I got you now. It is FileInputStream what you want. Because it is input, it will only doing an open on an existing, and so your construction will cause a FileNotFoundException if the file does not exist. – TA Nguyen Feb 26 '14 at 05:17
1

The FileNotFoundException is the correct one to catch, however I gather that you're worried about the stacks building up? I tested reading back the file after multiple failed attempts and it was fine. The recursive call is at the end of the method so it is the last line of code and therefore the stacks shouldn't have any effect.

However, if you want, you could use a while loop instead of recursion to avoid stack buildup:

public static void processSales() {

    Scanner scanFile = null;
    Scanner keyboard = new Scanner(System.in);

    while (scanFile == null) {

        System.out.println("Enter sales file name: ");
        String salesFile = keyboard.nextLine();

        try {
            scanFile = new Scanner(new File(salesFile));

            while (scanFile.hasNextLine()) {
                System.out.println(scanFile.nextLine());
            }
        } catch(FileNotFoundException fnfe) {
            System.out.println("Invalid file name supplied, please try again.");
        }
    }
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97