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