Counting letter occurrence

前端 未结 4 420
隐瞒了意图╮
隐瞒了意图╮ 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 05:54

    There is bug in your code.Correct code below-

       String fileName;    // Holds the name of the file
        String letter;      // Letter to search for in the file
    
        // Get the name of the file and character from the user
        fileName = "C:\\bin\\GWT.txt";
        letter = "X";
    
    
        // Open the file for reading
        File file = new File(fileName);         
        Scanner inputFile = new Scanner(file);  // Declare new scanner object for file reading
    
    
        // Set accumulator to zero
        int count = 0;
        while(inputFile.hasNext()) {
          if (inputFile.nextLine().toLowerCase().contains(letter.toLowercase())) { 
               count++;          // add letter occurrence
           }
        }
        System.out.println(count);
    

提交回复
热议问题