1

I want the user to enter a filename on the command line. If they don't enter any, I should print information says file was not processed and exit out. Here is my try catch block

try{
      parser.openFile(args[0]);
      if(parser.getCounter() == 3)
        {
          System.out.println("File was processed: true");
        }
        else
        {
          System.out.println("File was processed: false. Missing information.");
        }
        //found = true;
      }
        catch (IOException e)
          {
             e.printStackTrace();
             System.out.println("File was processed: false. Re-enter filename.");
             //fName = keyboard.nextLine();
        }

The openFile method is in my class, and it is here, in case anyone needs it:

  public void openFile (String filename)throws IOException{
    fis = new FileInputStream(filename);
    br = new BufferedReader(new InputStreamReader(fis));
    String thisLine;
    thisLine = br.readLine();
    while(thisLine != null)
    {
        lines.add(thisLine);
        thisLine = br.readLine();
    }
}

Somehow "File was processed: false. Re-enter filename." does not printed out when there is not filename in the command line. Can anyone help me please?

SLM
  • 25
  • 1
  • 4
  • Take a look at this: http://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java – Rana_S Nov 12 '15 at 02:49
  • 1
    what the error you see in the console ? – Albert Pinto Nov 12 '15 at 02:50
  • try checking length of args to decide if user has entered an argument, rather than depending onFileInputStream to throw an exception. – Jos Nov 12 '15 at 02:51
  • I can check that using if-else statement, but the professor wants us to use try-catch...how should I blend that into the try-catch block? – SLM Nov 12 '15 at 02:56
  • try to use catch (Exception e) instead of IOException and check the result. – josivan Nov 12 '15 at 03:02
  • Um...Exception e doesn't work as well.... – SLM Nov 12 '15 at 03:05

3 Answers3

2

Probably you're getting a different exception. Maybe

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

Try to validate the content of args

Example:

public class TesteArgs {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Inform a valid name...");
            System.exit(0);
        }
        // continue handling your args param
        System.out.println(args[0]);
    }
}
josivan
  • 1,661
  • 1
  • 12
  • 22
  • Yes that's the exception I am getting. I can check that using if-else statement, but professor wants us to use try catch, and I am not sure how to blend that into the try catch block... – SLM Nov 12 '15 at 02:57
0

Change your IOException to Exception in your catch statement, that'll get 'em exceptions caught nicely for ya.

Joel Min
  • 3,168
  • 1
  • 15
  • 35
0

You can catch multiple exception in try{}catch{} block.

You have to catch specific exceptions (ArrayIndexOutOfBoundsException, which is child of Exception) first and generic exception (Exception) later.

Sample code:

    try{
        // Your code
        File f = new File ("a.txt");
        FileInputStream fio = new FileInputStream(f);
    }catch(ArrayIndexOutOfBoundsException ae ){
        System.out.println(ae);
    }catch(IndexOutOfBoundsException ee ){
        System.out.println(ee); 
    }catch(IOException ioe ){
        System.out.println(ioe);    
    }catch(RuntimeException re ){
        System.out.println(re);
    }catch(Exception e ){
        System.out.println(e);      
    }catch(Throwable t ){
        System.out.println(t);      
    }

Have a look at hierarchy of exceptions

Ravindra babu
  • 42,401
  • 8
  • 208
  • 194