title: Java Sequential Search of a txt file
body: <p>This program searches a file for a given entry. I think my print statement in the main is incorrect. When I run the command prompt, my program prints out &quot;Item cat was NOT found!&quot; even though &quot;cat&quot; is present in the list the program checks. Here is the list:
  CAT
  RAT
  VAT
  MAT
  BAT
  HAT
  ZAT
  PAT
  SAT
  TAT</p>
  <p>Here is my program:</p>
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.Scanner;
  
  public class SequentialSearchFile {
      public static void main(String args[])
      {
          // String to contain filename
          String fileName = &quot;&quot;;
          // String to contain search item;
          String searchItem;
          Scanner s = new Scanner (System.in);
          
              // If there is one command-line parameter
              if (args.length == 1 )
              {
                  fileName = args[0]; // assume that it is the filename
                  System.out.print(&quot;Please enter search item: &quot;);
                  searchItem = s.nextLine();
              }
              // If there are two command-line parameters
              else if (args.length == 2)
              {
                  fileName = args[0]; // take them as the filename
                  searchItem = args[1]; // and as the search item
              }
              else {
                  System.out.print(&quot;Please enter file name: &quot;);// no command line parameters?
                  fileName = s.nextLine();                     // prompt user for both
                  System.out.print(&quot;Please enter search item: &quot;); 
                  searchItem = s.nextLine();
              }
              searchItem = searchItem.trim(); // trim any extra spaces from search item
              
              System.out.println(&quot;Searching file&quot; + fileName + &quot;...&quot;);
              System.out.println(&quot;Item &quot; + searchItem + &quot; was &quot; +
                                  (sequentialSearch(fileName, searchItem) ?&quot;&quot; : &quot;NOT &quot;) + &quot;found!&quot;);
      }
      
      // method opens file and searches the contents
      public static boolean sequentialSearch (String fileName, String searchItem) {           
          boolean result = false;                               // default response is not found
          try {
              Scanner f = new Scanner(new File(fileName));      // get a connection to the file
              String fileItem;
              while(f.hasNext())
                  {
                      fileItem = f.nextLine();                  // check each entry against the search item
                      if(searchItem.equalsIgnoreCase(fileName)) 
                      {
                          result = true;                       // if found, set result to true
                          break;                               // and break out of the while loop
                      }
                  }
              }
          catch(FileNotFoundException e) {                    // if the file wasn't there,
              System.out.println(e.toString());               // show the exception text
              System.exit(0);                                 // and exit
          }
          return result;                                      // return the result
      }
  }
  </code></pre>