0
        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
             }
        }

ワードカウンターが機能しません (既に文字数と行数を数えることができます)。私はそれを機能させるためにさまざまな方法を試しましたが、常に読み込まれたファイルの最後の行からの単語のみをカウントすることになります。最後の行だけでなく、すべての行を読み取る方法について何か提案はありますか? ありがとう

4

3 に答える 3

2

良い :

totalnumber.wordcounter += thewords.length

十分なはずです!

単語数を追加するのを忘れただけです...したがって、コード全体は次のとおりです。

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; 
    }

(何度も編集してすみません。時々、それはとても明白です... ;)

于 2013-01-21T23:39:25.143 に答える
1

必要なもの:

String [] thewords = thisline.split (" ");
totalnumber.wordcounter += thewords.length;

ループの外側で文字を繰り返します。+=の代わりに注意してください=

于 2013-01-21T23:39:43.033 に答える
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
于 2013-01-21T23:38:34.763 に答える