我的程序是假設計算忽略大寫和小寫的文件中每個字符的出現次數。我寫的方法是:打印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