title: Implementing binary search on an array of Strings
body: <p>I'm having a bit of trouble with this. The input array is based on file input and the size of the array is specified by the first line in the file. The binarySearch method seems to look alright but it doesn't seem to be working would. Anybody be able to help? Thanks.</p>
  
  <pre><code>public static int binarySearch(String[] a, String x) {
      int low = 0;
      int high = a.length - 1;
      int mid;
  
      while (low &lt;= high) {
          mid = (low + high) / 2;
  
          if (a[mid].compareTo(x) &lt; 0) {
              low = mid + 1;
          } else if (a[mid].compareTo(x) &gt; 0) {
              high = mid - 1;
          } else {
              return mid;
          }
      }
  
      return -1;
  }
  
  public static void main(String[] args) {
  
      Scanner input = new Scanner(System.in);
  
      System.out.println("Enter the name of your file (including file extension): ");
      String filename = input.next();
  
      String[] numArray;
      try (Scanner in = new Scanner(new File(filename))) {
          int count = in.nextInt();
  
          numArray = new String[count];
  
          for (int i = 0; in.hasNextInt() &amp;&amp; count != -1 &amp;&amp; i &lt; count; i++) {
              numArray[i] = in.nextLine();
          }
  
          for (int i = 0; i &lt; count; i++) //printing all the elements
          {
              System.out.println(numArray[i]);
          }
  
          String searchItem = "The";
  
          System.out.println("The position of the String is:");
          binarySearch(numArray, searchItem);
  
      } catch (final FileNotFoundException e) {
          System.out.println("That file was not found. Program terminating...");
          e.printStackTrace();
  
      }
  
  }
  </code></pre>