0
該方法返回一個數組,計算文檔中的行數,單詞和字符數。通過測試文件運行後,我仍然收到一些錯誤。計數單詞,字符和行數
public static int[] wc(Reader in) throws IOException {
int data = in.read();
int charcounter = 0;
int linecounter = 0;
int wordcounter = 0;
boolean previouswhitespace = false;
while (data != -1){
if (((char) data == '\n')){
linecounter++;
}
if (!(Character.isWhitespace((char) data))){
charcounter++;
if ((previouswhitespace == true) || (wordcounter == 0)){
previouswhitespace = false;
wordcounter++;
}
}
else if ((Character.isWhitespace((char) data))){
previouswhitespace = true;
}
data = in.read();
}
int[] array = {linecounter, wordcounter, charcounter};
return array;
}
''\ n''不是平臺無關的換行符 –
什麼錯誤?張貼在問題上。 –
另外,我敢肯定,將int數據轉換爲char不是執行字符解碼操作的正確方法。有很多可能的讀者和文件流,這會更好。 –