【问题标题】:Program won't enter try block inside while loop程序不会在 while 循环内进入 try 块
【发布时间】:2012-09-05 20:27:53
【问题描述】:

我正在尝试为学校编写程序。任务是循环并要求用户输入文件。当找不到文件或用户退出循环(在我的情况下,他们单击 joptionpane 上的取消)时,将输出每个文件的数据。到目前为止,这是我的代码:

/**
 * @param args
 */
public static void main(String[] args) {

    // Lists to hold file data
    List<String> fileNames = new ArrayList<String>();
    List<Integer> numChars = new ArrayList<Integer>();
    List<Integer> numWords = new ArrayList<Integer>();
    List<Integer> numLines = new ArrayList<Integer>();
    String inFile;

    while ( (inFile = JOptionPane.showInputDialog("Enter a file name:")) != null) {

        // Create new file object instance
        File file = new File(inFile);

        try {
            fileNames.add(inFile);  // File name as a string
            // Pass file objects
            numChars.add( getNumChars(file));
            numWords.add( getNumWords(file));
            numLines.add( getNumLines(file));
            System.out.println("entered try block");
        } catch(FileNotFoundException e) {
            System.out.println("Could not find file: " + inFile);
            // break out the loop and display data
            break;
        }
    }   // end while

    if (numChars.size() > 0)
        showFileData(fileNames, numChars, numWords, numLines);

    System.out.println("Program ended");

}

private static void showFileData(List<String> fileNames,
        List<Integer> numChars, List<Integer> numWords,
        List<Integer> numLines) {
    for (int i=0;i<numChars.size();i++) {
        System.out.println("Data for: " + fileNames.get(i));
        System.out.println("Number of characters: " + numChars.get(i));
        System.out.println("Number of words: " + numWords.get(i));
        System.out.println("Number of lines: " + numLines.get(i));
        System.out.println("-----------------------------------------------");
    }

}

private static int getNumLines(File file) throws FileNotFoundException {
    int c = 0;
    Scanner f = new Scanner(file);
    while(f.hasNextLine()) {
        c++;
    }
    return c;
}

private static int getNumWords(File file) throws FileNotFoundException {
    int c = 0;
    Scanner f = new Scanner(file);
    while (f.hasNextLine()) {
        String[] w = f.nextLine().split(" ");
        c += w.length;
    }
    return c;
}

private static int getNumChars(File file) throws FileNotFoundException {
    Scanner f = new Scanner(file);
    int c = 0;
    String line;
    while(f.hasNextLine()) {
        line = f.nextLine();
        String[] s = line.split(" ");
        for (int i=0;i<s.length;i++) {
            c += s[i].length();
        }
    }
    return c;
}

当我运行程序时,如果我按下取消,它会退出循环并在最后显示应该显示的内容。如果我输入一个不存在的文件,它会按预期工作。唯一不起作用的是当我输入一个确实存在的文件时,它不会弹出另一个输入对话框。实际上,它似乎并没有进入 try 块。我的程序设置是否正确?

【问题讨论】:

  • 在 try 块的开头和 try 块之前将一些输出打印到屏幕上。这样您就可以跟踪程序的执行情况,并查看当您输入正确的文件名时会发生什么。打印一个有用的东西是inFile 变量,只是为了确保它是您认为的那样
  • 你可以使用 File.isFile() 而不是使用异常。

标签: java exception loops while-loop try-catch


【解决方案1】:

getNumLines() 中有一个无限循环。您需要在 while 循环中使用 f.nextLine()

【讨论】:

    【解决方案2】:

    在 getNumLines 中:

     while(f.hasNextLine()) {
      c++;
     }
    

    将永远是真的,改变它

        private static int getNumLines(File file) throws FileNotFoundException {
        int c = 0;
        Scanner f = new Scanner(file);
         while(f.hasNextLine()) {
            f.nextLine();
            c++;
         }
         return c;
        }
    

    【讨论】:

      【解决方案3】:

      应用程序在这个方法中永远循环:

      private static int getNumLines(File file) throws FileNotFoundException {
          int c = 0;
          Scanner f = new Scanner(file);
          while(f.hasNextLine()) {
              c++;
          }
          return c;
      }
      

      注意 while() 循环:您检查文件中是否还有其他行,但不要从文件中读取任何数据 - 因此,hasNextLine() 会不断返回 true。

      【讨论】:

        【解决方案4】:

        @DomV 是正确的,您的 getNumLines() 函数中有一个无限循环。

        您最终打开文件并阅读它三次以获取行数,一次获取单词数,一次获取字符数。也许您想找到一种方法来最小化那些昂贵的磁盘操作。这样做还会迫使您在阅读文件并解决无限循环时更仔细地检查您正在做的事情。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-11
          • 2013-11-16
          • 2015-03-08
          • 2016-01-11
          • 1970-01-01
          • 1970-01-01
          • 2018-07-18
          • 2022-11-02
          相关资源
          最近更新 更多