title: Scanner only searching first line of .txt file
body: <p>I'm in a beginning programming class, and seem to be having a major issue with searching a text file. What my code should do, based on the assignment:</p>
  
  <ul>
  <li>Accept input, in this case a name and place that input into a .txt file</li>
  <li>Allow the user to search for a name, or part of a name, and return all lines with matching text. </li>
  </ul>
  
  <p>I have the input portion of the assignment complete, and am on the verge on completing the retrieval portion, but my code only searches the first line of the .txt file. I am able to print out all lines of the .txt file, and if I search for the name in Line 1 of the .txt file, it will print the line correctly. My issue comes when I am searching for a name that is not on Line 1. Below is my code:</p>
  
  <pre><code>System.out.println ("Would you like to retrieve names from your index? (YES/NO)");
  try
  {
      retrieve=input.readLine();
     }
     catch (IOException E)
     {
      System.out.println(E);
     }
    }
   if (choice == 2 &amp;&amp; retrieve.equalsIgnoreCase("YES") || retrieve.equalsIgnoreCase("Y"))
    {
     while (retrieve2.equalsIgnoreCase("YES") || retrieve2.equalsIgnoreCase("Y"))
     {
       FileReader reader = new FileReader("Name_Index.txt");
       BufferedReader bufferedReader = new BufferedReader(reader);
       String line = bufferedReader.readLine();
  
       System.out.println ("Enter a string of characters in which to search by or enter \"all names\" f$
       search_term = gatherInput();
  
       System.out.println("Search results include: ");
       ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
       Scanner inFile = new Scanner (new File("Name_Index.txt"));
       inFile.useDelimiter(",");
  
       while (inFile.hasNextLine())
       {
        list.add(inFile.nextLine());
       }
       Collections.sort(list);    
       if (search_term.equalsIgnoreCase("all names"))
       {
        for (String temp : list)
        {
         System.out.println(temp);
        }
       }
       else if (line.toLowerCase().contains(search_term.toLowerCase()))
       {
         System.out.println(line);     
         bufferedReader.close();
       }
  System.out.println("End!");
      System.out.println ("Would you like to retrieve names from your index? (YES/NO)");
      try
      { 
       retrieve2=input.readLine();
      }
      catch (IOException E)
      {
       System.out.println(E);
      }
     }   
      System.out.println("Thank you, come again!");
    }
   }    
     public static String gatherInput()
     {
      Scanner scan = new Scanner(System.in);
      String user_input = scan.nextLine();
      return user_input;
     }
    }
  </code></pre>
  
  <p>I have tried expanding the <em>while (inFile.hasNextLine())</em> loop to include the second "if" statement, however that creates an issue for the "all names" search - it returns the entire list multiple times (however many lines are in the file). I have even tried creating another <em>while (inFile.hasNextLine())</em> loop within the second "if" statement, and there is no difference in outcome. </p>
  
  <p>I'm so frustrated at this point, because I've been working on this code for over a week, and have reviewed all of my notes and lecture recordings for this assignment with no help. Any insight would be much appreciated.</p>