代码编译并运行,但一部分不能正确循环

时间:2017-10-25 01:14:39

标签: java exception

这是我的代码:

import java.io.*;

import java.util.Scanner;

import java.text.DecimalFormat;

public class QuestionTwo 
{

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

 String file;
  Float number;
  String File;
  double avgnumbers;

  DecimalFormat formatter = new DecimalFormat("##.##");

  Scanner keyboard = new Scanner(System.in);

  System.out.println("Please enter a file name");

  file = keyboard.nextLine();

  FileReader freader = new FileReader(file);

  BufferedReader inputFile = new BufferedReader(freader);

  while(file != ("input.txt"))

  {
   System.out.println("File is not found, enter the filename again.");

    file = keyboard.nextLine();

  }

  System.out.println("The number to check: ");

  number = keyboard.nextFloat();

  String inp = inputFile.readLine();

  int count = 0;

  int sum = 0;

  while (inp != null)

 {
      int strnumber = Integer.parseInt(inp);

      if(strnumber > number)

      {
       sum += strnumber;

       count++;
      }

     inp = inputFile.readLine();
  }

  avgnumbers = (double)sum / count;

  System.out.println("The average of the numbers that are greater than " + number + " is " + avgnumbers);

  inputFile.close();
}
}

我遇到这个代码的问题是,当我为第一个问题写错了文件名时,我得到了一个例外。我编写的是它告诉我那是错的,我应该重新输入文件名,以便它可以这样做。理论上它会无限地这样做,直到输入正确的文件名(input.txt);我想指出,我的代码的结果是我喜欢并满足其要求;只是它一直显示这个异常。它在代码的另一部分要求提供文件名时做了这件事,所以这可能与此有关。虽然在我改变任何事情之前我想要得到一些"同行评审&#34 ;;任何帮助都会受到高度赞赏。

异常堆栈:

[DrJava Input Box]
java.io.FileNotFoundException: dxfgb (The system cannot find the file 
specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at QuestionTwo.main(QuestionTwo.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at 

edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.jav      一:267)

  

1 个答案:

答案 0 :(得分:2)

你得到一个例外:

Exception in thread "main" java.io.FileNotFoundException: abc123 (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at QuestionTwo.main(QuestionTwo.java:18)

输入不存在的文件名,因为您在检查输入的文件名之前是否尝试创建FileReader,然后检查该文件是否存在。 FileReader(String)的Javadoc指定如果指定的文件不存在,将抛出FileNotFoundException

要解决此问题,同时仍然拥有反映您明显意图的代码,请更改第一个while循环的条件:

while(file != ("input.txt"))

为:

while (!(new File(file).exists()))

并移动:

FileReader freader = new FileReader(file);
BufferedReader inputFile = new BufferedReader(freader);
在第一个while循环之后

,以便仅为现有文件名创建FileReader