count characters, words and lines in file

后端 未结 8 1751
离开以前
离开以前 2020-12-18 16:47

This should count number of lines, words and characters into file.

But it doesn\'t work. From output it shows only 0.

Code:

8条回答
  •  执念已碎
    2020-12-18 17:38

    You could store every line in a List and then linesCount = list.size().

    Calculating charsCount:

    for(final String line : lines)
        charsCount += line.length();
    

    Calculating wordsCount:

    for(final String line : lines)
        wordsCount += line.split(" +").length;
    

    It would probably be a wise idea to combine these calculations together as opposed to doing them seperately.

提交回复
热议问题