count characters, words and lines in file

后端 未结 8 1771
离开以前
离开以前 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:32

    Use Scanner methods:

    int lines = 0;
    int words = 0;
    int chars = 0;
    while(in.hasNextLine()) {
        lines++;
        Scanner lineScanner = new Scanner(in.nextLine());
        lineScanner.useDelimiter(" ");
        while(lineScanner.hasNext()) {
            words++;
            chars += lineScanner.next().length();
        }
    }
    

提交回复
热议问题