Search code examples
javajava.util.scannerbufferedreader

Scanner only searching first line of .txt file


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:

  • Accept input, in this case a name and place that input into a .txt file
  • Allow the user to search for a name, or part of a name, and return all lines with matching text.

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:

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 && 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<String> list = new ArrayList<String>();
     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;
   }
  }

I have tried expanding the while (inFile.hasNextLine()) 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 while (inFile.hasNextLine()) loop within the second "if" statement, and there is no difference in outcome.

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.


Solution

  • You are reading only 1 line of the file

    String line = bufferedReader.readLine();
    

    Why don't you read all lines and store them in a List;

    List<String> lines = new ArrayList<>();
    String line = bufferedReader.readLine();
    while(line != null){
       lines.add(line);
       line = bufferedReader.readLine();
    }
    bufferedReader.close();
    

    Then to print all lines containing a substring ignorecase:

    lines.stream().filter(l -> l.toLowerCase().contains(search_term.toLowerCase))
    .forEach(s -> System.out.println(s));