2

我试图从用户“输入”文件中计算行数、单词数、字符数。
在这个节目计数并继续询问之后。

如果文件不存在,则打印在运行期间计算的所有数据。

代码:

public class KeepAskingApp {
    private static int lines;
    private static int words;
    private static int chars;

public static void main(String[] args) {
    boolean done = false;
    //counters
    int charsCount = 0, wordsCount = 0, linesCount = 0;
    Scanner in = null;  
    Scanner scanner = null;
    while (!done) {
        try {
            in = new Scanner(System.in);
            System.out.print("Enter a (next) file name: ");
            String input = in.nextLine();

            scanner = new Scanner(new File(input));     
            while(scanner.hasNextLine()) {
                lines += linesCount++;
                Scanner lineScanner = new Scanner(scanner.nextLine());
                lineScanner.useDelimiter(" ");
                while(lineScanner.hasNext()) {
                    words += wordsCount++;
                    chars += charsCount += lineScanner.next().length();
                }
                System.out.printf("# of chars: %d\n# of words: %d\n# of lines: ", 
                        charsCount, wordsCount, charsCount);

                lineScanner.close();
            }

            scanner.close();
            in.close();
        } catch (FileNotFoundException e) {
            System.out.printf("All lines: %d\nAll words: %d\nAll chars: %d\n", 
                    lines, words, chars);
            System.out.println("The end");
            done = true;
        } 
    }
}

}

但我不明白为什么它总是显示没有参数的输出:

All lines: 0
All words: 0
All chars: 0
The end

为什么它省略了所有内部部分。
可能是因为我使用的扫描仪很少,但看起来一切正常。有什么建议么?

更新:

感谢所有提供一些提示的人。我用新的信息重新思考所有构造和重写的代码。
为了避免棘手的扫描仪输入线,我使用了JFileChooser

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;

public class KeepAskingApp {
    private static int lines;
    private static int words;
    private static int chars;

    public static void main(String[] args) {
        boolean done = false;
        // counters
        int charsCount = 0, wordsCount = 0, linesCount = 0;
        Scanner in = null;
        Scanner lineScanner = null;
        File selectedFile = null;
        while (!done) {
            try {
                try {
                    JFileChooser chooser = new JFileChooser();

                    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                        selectedFile = chooser.getSelectedFile();
                        in = new Scanner(selectedFile);
                    }
                    while (in.hasNextLine()) {
                        linesCount++;
                        lineScanner = new Scanner(in.nextLine());
                        lineScanner.useDelimiter(" ");
                        while (lineScanner.hasNext()) {
                            wordsCount++;
                            charsCount += lineScanner.next().length();
                        }

                    }
                    System.out.printf(
                            "# of chars: %d\n# of words: %d\n# of lines: %d\n",
                            charsCount, wordsCount, linesCount);

                    lineScanner.close();
                    lines += linesCount;
                    words += wordsCount;
                    chars += charsCount;

                    in.close();
                } finally {
                    System.out.printf(
                            "\nAll lines: %d\nAll words: %d\nAll chars: %d\n",
                            lines, words, chars);
                    System.out.println("The end");
                    done = true;
                }
            } catch (FileNotFoundException e) {
                System.out.println("Error! File not found.");
            }
        }
    }
}
4

2 回答 2

2

几个问题(实际上您的代码有很多问题,但我将解决与您发布的输出直接相关的问题):

首先,块中的东西只有在你得到一个;catch时才会发生。FileNotFoundException在那里可以处理错误并从错误中恢复。我怀疑您打算在finally那里放置一个块,或者您打算在catch. 我建议阅读这篇关于捕获和处理异常的教程,它直接描述了trycatchfinally.

阅读该教程后,请回到您的代码;你可能会发现你有一些重组工作要做。

其次,考虑到上述情况,您看到的输出很明显您正在执行该catch块中的代码,这意味着您正在获得一个FileNotFoundException. 这可能是由以下两种(可能很明显)之一引起的:

  1. 你输入的文件,嗯,没有找到。它可能不存在,也可能不在您期望的位置。检查以确保您输入了正确的文件名并且该文件确实存在。

  2. input字符串不是您所期望的。也许您从以前的输入中读取了一个空白行,等等。

解决原因 2:如果输入缓冲区中已经有换行符,无论出于何种原因,您将读取一个带有Scanner. 您可能希望input在打开文件之前打印 的值,以确保它是您所期望的。

如果您看到空白行,请跳过它们。所以,而不是这个:

String input = in.nextLine();
scanner = new Scanner(new File(input));     

像这样的东西将不受空行的影响:

String input;
do {
    input = in.nextLine().trim(); // remove stray leading/trailing whitespace
} while (input.isEmpty()); // keep asking for input if a blank line is read
scanner = new Scanner(new File(input));

最后,我认为您可以找出在输出中看到 0 的原因。当您尝试打开文件new Scanner(new File(input));但由于找不到文件而失败时,它会引发异常,并且程序会立即跳转到您的catch块中的代码。这意味着lines,wordschars的初始值仍然为零(所有修改它们的代码都被跳过了)。

希望有帮助。

于 2013-08-17T12:04:55.337 回答
1

println()的 s 在一个catch块中

    } catch (FileNotFoundException e) {
        System.out.printf("All lines: %d\nAll words: %d\nAll chars: %d\n", 
                lines, words, chars);
        System.out.println("The end");
        done = true;
    }

这意味着你抓住了一个FileNotFoundException. 我想你可以从这里弄清楚。

于 2013-08-17T11:58:56.153 回答