【问题标题】:Count all characters in a file including \n etc计算文件中的所有字符,包括 \n 等
【发布时间】:2013-07-18 04:09:19
【问题描述】:

我正在尝试遍历一个 txt 文件并计算所有字符。这包括 \n 换行符和其他任何内容。我只能通读一次文件。我还在记录字母频率、行数、字数等。我不太清楚在哪里计算字符总数。 (见下面的代码)我知道我需要在使用 StringTokenizer 之前。 (顺便说一句,我必须使用它)。我尝试了多种方法,但无法完全弄清楚。任何帮助,将不胜感激。提前致谢。注意* 我的变量 numChars 只计算字母字符(a、b、c 等)编辑发布类变量以更了解代码

private final int NUMCHARS = 26;
private int[] characters = new int[NUMCHARS];
private final int WORDLENGTH = 23;
private int[] wordLengthCount = new int[WORDLENGTH];
private int numChars = 0;
private int numWords = 0;
private int numLines = 0;
private int numTotalChars = 0;
DecimalFormat df = new DecimalFormat("#.##");

public void countLetters(Scanner scan) {
    char current;
    //int word;
    String token1;

    while (scan.hasNext()) {

        String line = scan.nextLine().toLowerCase();
        numLines++;

        StringTokenizer token = new StringTokenizer(line,
            " , .;:'\"&!?-_\n\t12345678910[]{}()@#$%^*/+-");
        for (int w = 0; w < token.countTokens(); w++) {
            numWords++;
        }

        while (token.hasMoreTokens()) {
            token1 = token.nextToken();
            if (token1.length() >= wordLengthCount.length) {
                wordLengthCount[wordLengthCount.length - 1]++;
            } else {
                wordLengthCount[token1.length() - 1]++;

            }

        }
        for (int ch = 0; ch < line.length(); ch++) {
            current = line.charAt(ch);
            if (current >= 'a' && current <= 'z') {
                characters[current - 'a']++;
                numChars++;

            }
        }
    }
}

【问题讨论】:

    标签: java java.util.scanner stringtokenizer


    【解决方案1】:

    使用string.toCharArray(),类似:

    while (scan.hasNext()) {
        String line = scan.nextLine();
        numberchars += line.toCharArray().length;
        // ...
    }
    

    另一种方法是直接使用string.length:

    while (scan.hasNext()) {
        String line = scan.nextLine();
        numberchars += line.length;
        // ...    
    }
    

    使用 BfferedReader,您可以像 this 一样:

    BufferedReader reader = new BufferedReader(
        new InputStreamReader(
            new FileInputStream(file), charsetName));
    int charCount = 0;
    while (reader.read() > -1) {
        charCount++;
    }
    

    【讨论】:

    • String.length 几乎可以工作。如果我使用 scan.nextLine().length 它会在线程“main”java.util.NoSuchElementException 中引发运行时异常错误:在 java.util.Scanner.nextLine(Scanner.java:1533) 处找不到行。在 TextStatistics.countLetters(TextStatistics.java:28) 在 ProcessText.main(ProcessText.java:25)--- 而我只是使用 numTotalChars += line.length();这几乎就在那里。它仍然没有捕获 \n (换行符),但我认为我可以每次添加 1,因为它是每行捕获的。
    • 这个答案也很有帮助。不过,对于这个项目,我不允许使用缓冲阅读器。但无论如何,这是很好的知识。感谢您的评论。
    【解决方案2】:

    我会使用 BufferedReader 从文件中读取字符并使用 Guava Multiset 来计算字符数

    BufferedReader rdr = Files.newBufferedReader(path, charSet);
    HashMultiset < Character > ms = HashMultiset.create();
    for (int c;
    (c = rdr.read()) != -1;) {
        ms.add((char) c);
    }
    for (Multiset.Entry < Character > e: ms.entrySet()) {
        char c = e.getElement();
        int n = e.getCount();
    }
    

    【讨论】:

    • 这是有道理的,但在规范中我们不能使用 BufferedReader。但是,感谢您的输入。它让我思考。 :)
    猜你喜欢
    • 2011-11-06
    • 2019-08-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多