maymay
maymay

Reputation: 1

java keep trying until there are no more filenotfoundexception

I was trying to write code which would read an input file and create an output file. But when I tried to add a try until a correct input file name is input, I had problems. It shows not proper filenotfound exception is in try....

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

          //prompt for the input file name
          Scanner in = new Scanner(System.in);
          //keep trying until there are no more exceptions
          //boolean done = false;
          String inputfilename = " ";
          while (!done)
          {
             try
             {
               System.out.print("Input file name (from your computer): ");
               inputfilename = in.next();
               done = true;
             }
             catch (FileNotFoundException exception)
             {
               System.out.println("****** ERROR ******\nCannot locate the input file '" + inputfilename + "' on your computer - please try again.");
             }
          }
          //prompt for the output file name
          System.out.print("What would you like to call your output file: ");
          //use outputfilename variable to hold input value;
          String outputfilename = in.next();
          //construct the Scanner and PrintWriter objects for reading and writing
          File inputfile = new File(inputfilename);
          Scanner infile = new Scanner(inputfile);
          PrintWriter out = new PrintWriter(outputfilename);
          //read the input and write the output
          out.println("Here is the class average for mstu4031:\n");
          double totalgrade = 0;
          double number = 0;
          while (infile.hasNextDouble())
          {
             double grade = infile.nextDouble();
             out.println("\n");
             out.printf("%.1f\n",grade);
             number++;
             totalgrade = totalgrade + grade;
          }
          //print numbers and average in output file
          out.println("\n\n");
          out.printf("\nNumber of grades: %.1f",number);
          //calculate average
          double average = totalgrade/number;
          out.println("\n\n");
          out.printf("\nAverage: %.2f",average);

          finally
          {     
          in.close();
          out.close();
          }
}

Upvotes: 0

Views: 534

Answers (4)

PramodHegde
PramodHegde

Reputation: 19

Opening a file may throw an Exception. That's Why you need to put them inside try block. You have put only reading the input part inside try-catch block

Hope this code works properly:

//prompt for the input file name
  Scanner in = new Scanner(System.in);
  //keep trying until there are no more exceptions
  //boolean done = false;
  String inputfilename = " ";
  while (!done)
  {
     try
     {
       System.out.print("Input file name (from your computer): ");
       inputfilename = in.next();
       done = true;
       //prompt for the output file name
       System.out.print("What would you like to call your output file: ");
       //use outputfilename variable to hold input value;
       String outputfilename = in.next();
      //construct the Scanner and PrintWriter objects for reading and writing
      File inputfile = new File(inputfilename);
      Scanner infile = new Scanner(inputfile);
      PrintWriter out = new PrintWriter(outputfilename);
      //read the input and write the output
      out.println("Here is the class average for mstu4031:\n");
      double totalgrade = 0;
     double number = 0;
     while (infile.hasNextDouble())
     {
     double grade = infile.nextDouble();
     out.println("\n");
     out.printf("%.1f\n",grade);
     number++;
     totalgrade = totalgrade + grade;
   }
    //print numbers and average in output file
    out.println("\n\n");
    out.printf("\nNumber of grades: %.1f",number);
    //calculate average
    double average = totalgrade/number;
    out.println("\n\n");
    out.printf("\nAverage: %.2f",average);
     }
     catch (FileNotFoundException exception)
     {
       System.out.println("****** ERROR ******\nCannot locate the input file '" + inputfilename + "' on your computer - please try again.");
     }
  }
  finally
  {     
  in.close();
  out.close();
  }

Upvotes: 0

MC Emperor
MC Emperor

Reputation: 23057

You can only catch an exception if something in the try block may throw an exception.

However, you should test for existence of a file with File.exists(), instead of catching an exception.

File file;
do {
    System.out.print("Input file name (from your computer): ");
    file = new File(in.next());
} while (!file.exists());

Upvotes: 0

Luminous_Dev
Luminous_Dev

Reputation: 614

Wrong here. You are only receiving input without checking if the file actually exist. Every valid inputs will let you get out of the loop.

       if(new File(inputfilename).exist()){
           done = true;
       }else{
           System.out.println("****** ERROR ******\nCannot locate the input file '" + inputfilename + "' on your computer - please try again.");
       }

Upvotes: 0

D. Schmidt
D. Schmidt

Reputation: 167

There is no method in your try block that may throw a FileNotFoundException.

Try to instantiate your Scanner in the try block. It will throw the expected FileNotFoundException if the filename read from stdin does not exist:

String inputfilename = null;
Scanner infile = null;
while (!done)
{
   try
   {
     System.out.print("Input file name (from your computer): ");
     inputfilename = in.next();
     infile = new Scanner(new File(inputfilename));
     done = true;
   }
   catch (FileNotFoundException exception)
   {
     System.out.println("****** ERROR ******\nCannot locate the input file '" + inputfilename + "' on your computer - please try again.");
   }
}

Upvotes: 1

Related Questions