while (scan.hasNextLine()) {
String thisline = scan.nextLine();
totalnumber.countthelines++; //linecount works
for(int i = 0; i < thisline.length();i++){
totalnumber.charactercounter++; //chararacter count works
String [] thewords = thisline.split (" ");
totalnumber.wordcounter = thewords.length; //does not work
}
}
我无法让我的wordcounter工作(我已经能够计算字符和行数了)。我已经尝试了许多不同的方法使它工作,但它总是最终只计算读入文件的最后一行的单词。关于如何让它读取每一行而不是最后一行的任何建议? 感谢
答案 0 :(得分:2)
嗯:
totalnumber.wordcounter += thewords.length
应该够了!
你只是忘了添加字数...... 所以entiere代码是:
while (scan.hasNextLine()) {
String thisline = scan.nextLine();
totalnumber.countthelines++; //linecount works
totalnumber.charactercounter+=thisline.length(); //chararacter count works
String [] thewords = thisline.split (" ");
totalnumber.wordcounter += thewords.length;
}
(抱歉有多处编辑。有时候,这很明显......;)
答案 1 :(得分:1)
你需要:
String [] thewords = thisline.split (" ");
totalnumber.wordcounter += thewords.length;
在循环之外迭代字符。请注意+=
而不是=
。
答案 2 :(得分:0)
for(int i = 0; i < thisline.length(); i++) {
totalnumber.charactercounter++; //chararacter count works
}
String [] thewords = thisline.split (" ");
totalnumber.wordcounter = thewords.length; //does not work