count characters, words and lines in file

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

    Looks like everyone is suggesting you an alternative,

    The flaw with your logic is, you are not looping through the all the characters for the entire line. You are just looping through the first character of every line.

     ch = in.next().charAt(0);
    

    Also, what does 2 in charsCount -= linesCount * 2; represent?

    You might also want to include a try-catch block, while accessing a file.

      try {
                in = new Scanner(selectedFile);
            } catch (FileNotFoundException e) {}
    

提交回复
热议问题