我正在寻找对文本文件执行字符计数,然后用它与其余字符的相对频率显示每个字符,但我现在只得到空白的控制台。任何帮助将不胜感激。
import java.io.*;
public class RelativeFrequency {
public static void main(String[] args) throws IOException {
File file1 = new File("Rf.txt");
BufferedReader in = new BufferedReader (new FileReader (file1));
System.out.println("Letter Frequency");
int nextChar;
char ch;
int[] count = new int[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z')
count[ch - 'a']++;
}
for (int i = 0; i < 26; i++) {
System.out.printf("", i + 'A', count[i]);
}
in.close();
}
}
答案 0 :(得分:3)
您的printf语句格式不正确
System.out.printf("%c %d", i + 'A', count[i]);
答案 1 :(得分:3)
您的printf
错了
// Assuming you want each letter count on one line
System.out.printf("%c = %d\n", i + 'A', count[i]);
在与tolower
if (ch >= 'a' && ch <= 'z')
ch = Character.toLowerCase(ch);