title: Count number of times string appears in a file
body: <p>I'm trying to count the number of times a certain string appears in a text file (.txt), and so far, I got FileReader to work (it reads the file, and I can output the text in the file to the console). What I'm trying to do know is check if a string (inputted by a user) exists in the text file. What I have so far is this:</p>
  
  <pre><code>import java.io.*;
  import java.util.Scanner;
  
  public class Assignment2 {
  
      String fileName = "test.txt";
      String line = null;
      int counter = 0;
  
      public String getUserInput() {
          String userInput;
          Scanner userInputScanner = new Scanner(System.in);
          userInput = userInputScanner.nextLine();
          return userInput;
      }
  
      public String returnUserInput() {
          String userInput = getUserInput();
          return userInput;
      }
  
  
      public void readFile() {        
          try {
  
              FileReader fileReader = new FileReader(fileName);
              BufferedReader bufferedReader = new BufferedReader(fileReader);
  
              while((line = bufferedReader.readLine()) != null) {
                  while (line.indexOf(word, indexOfWord) != -1) {
                      indexOfWord += word.length();
                      counter += 1;
                  }
              }
              bufferedReader.close();
  
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          Assignment2 test = new Assignment2();
          test.readFile();
      }
  
  }
  </code></pre>
  
  <p>Whats happening is that I can't loop through the file, and when I run the program with the for loop, it gets stuck(?), it just keeps running with no errors, but it does nothing.</p>
  
  <p>I know that my problem is with the for loop (line.length() won't work, because the buffered reader reads one line at a time... what I really want is to get the number of strings in the file). I'm also having trouble checking user input (I want to ignore the case of the letters, but I cannot do that with .contains(), and I can't think of another way to check if the line contains the user input).</p>
  
  <p>Also, is my though process correct, or is the way I'm doing this not efficient? I have to check a 3000 word document.</p>