【发布时间】:2010-03-31 23:22:58
【问题描述】:
我的程序假设忽略大小写,计算文件中每个字符的出现次数。我写的方法是:
public int[] getCharTimes(File textFile) throws FileNotFoundException {
Scanner inFile = new Scanner(textFile);
int[] lower = new int[26];
char current;
int other = 0;
while(inFile.hasNext()){
String line = inFile.nextLine();
String line2 = line.toLowerCase();
for (int ch = 0; ch < line2.length(); ch++) {
current = line2.charAt(ch);
if(current >= 'a' && current <= 'z')
lower[current-'a']++;
else
other++;
}
}
return lower;
}
并使用以下方式打印出来:
for(int letter = 0; letter < 26; letter++) {
System.out.print((char) (letter + 'a'));
System.out.println(": " + ts.getCharTimes(file));
}
其中 ts 是我之前在 main 方法中创建的 TextStatistic 对象。但是,当我运行我的程序时,它不会打印出字符出现的频率:
a: [I@f84386
b: [I@1194a4e
c: [I@15d56d5
d: [I@efd552
e: [I@19dfbff
f: [I@10b4b2f
而且我不知道自己做错了什么。
【问题讨论】:
标签: java printing for-loop character