Counting letter occurrence

前端 未结 4 442
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  萌比男神i
    2021-01-17 06:13

    This answer is assuming each line in your text file contains just one letter as your question suggests.

    You need to wrap your if statement in a loop as currently you are only checking the first line of the file:

         while(inputFile.hasNext()) {
             if (inputFile.nextLine().equalsIgnoreCase(letter))    {                                
    
                 count++;          // add letter occurrence
    
                 total += count;   // add the letter occurrence to the total
             }
         }
    

    Also you can replace:

    count++;
    total+= count;
    

    with just

    total++;
    

提交回复
热议问题