2

我正在尝试遍历一个 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++;

            }
        }
    }
}
4

2 回答 2

0

使用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 你可以这样

BufferedReader reader = new BufferedReader(
    new InputStreamReader(
        new FileInputStream(file), charsetName));
int charCount = 0;
while (reader.read() > -1) {
    charCount++;
}
于 2013-07-18T04:25:28.983 回答
0

我会用 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();
}
于 2013-07-18T04:26:14.140 回答