java 继续尝试,直到不再有 filenotfoundException

标签 java input ioexception

我试图编写可以读取输入文件并创建输出文件的代码。但是当我尝试添加尝试直到输入正确的输入文件名时,我遇到了问题。它显示尝试中存在不正确的 filenotfound 异常....

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();
          }
}

最佳答案

您的 try block 中没有可能引发 FileNotFoundException 的方法。

尝试在 try block 中实例化您的扫描仪。如果从 stdin 读取的文件名不存在,它将抛出预期的 FileNotFoundException:

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.");
   }
}

关于java 继续尝试,直到不再有 filenotfoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41068748/

相关文章:

python - 一会儿监听输入,并发启动一个线程

c - 重新提示用户,直到他/她输入大于 1 的正整数值

c# - 为什么在使用 Serilog 日志记录时会出现 Serilog SelfLog 错误?

java - Jsoup无法打开html页面Status=-1

java - IOException 后重新启动 ServerSocket

java - 在服务器上实现 java webstart 的正确方法是什么?

java - 在Java中随机化一个字符串

html - 如果空白则在输入下划线

javascript - JSON 数字是否与 JavaScript 的数字相关联?

Java GAE : Can I filter by comparing the properties of entities?