Counting letter occurrence

前端 未结 4 418
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 05:19

The program needs to count and display the number of times that the specified charector appears in the text file.

Currently turning up zero for the total. I\'m not i

4条回答
  •  暖寄归人
    2021-01-17 06:14

        BufferedReader reader = new BufferedReader(new FileReader("somefile.txt"));
        int ch;
        char charToSearch='a';
        int counter=0;
        while((ch=reader.read()) != -1) {
            if(charToSearch == (char)ch) {
                counter++;
            }
        };
        reader.close();
    
        System.out.println(counter);
    

    Is this of any help?

提交回复
热议问题