我的程序是假设计算忽略大写和小写的文件中每个字符的出现次数。我写的方法是:打印int []时为什么会出现垃圾输出?
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是在我的主要方法之前创建一个TextStatistic
对象。然而,当我运行我的程序,而不是打印出来的字出现的频率是多少打印:
a: [[email protected]
b: [[email protected]
c: [[email protected]
d: [[email protected]
e: [[email protected]
f: [[email protected]
而且我不知道我做错了。
谢谢!像魅力一样工作! – Kat 2010-03-31 23:28:00
和什么smink回答 – Nishu 2010-04-01 00:26:36