title: binary search cant find the correct key
body: <p>ok, so I coded a program in java that reads the values from a file and stores them in an array and then pass them to binary class where the binary code will search for the key. Now if the key is listed in the file it will show the location and the key but if the key is not listed it will output the key is not listed. 
  my program works fine if my key is say 6474 or i believe until index reaches 500 but once the key is located (7988) it shows the key is not listed even though the key is in the file. Any help would be appreciated.....</p>
  
  <p>p.s. i have 667 values in my file and my key is 7988. no, it does not throws any error.</p>
  
  <p>here's the code:</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Scanner;
  
  
  public class MyMain {
  
  public static void main(String[] args) throws IOException {
  
      int key = 8681;
      int num;
      int phySize = 5000;
      int logSize = 0;
      int[] anArray = new int[phySize];
  
      File myFile = new File("integers");
      Scanner sc = new Scanner(myFile);
  
      while (sc.hasNext()) {
          num = sc.nextInt();
          System.out.println(" logSize " + num);
          anArray[logSize] = num;
          logSize = logSize + 1;
      }
  
      // System.out.println(" The log size is " + logSize);
  
      BinSearch Bin = new BinSearch();
      SeqSearch Seq = new SeqSearch();
  
  
  
      int count = -1;
  
      while (count &lt; logSize) {
          count++;
          if (anArray[count] == key) {
              break;
          }
      }
  
      int j = Bin.BinarySearch(anArray, logSize, key);
      int k = Seq.sequentialSearch(anArray, logSize, key);
      //System.out.println(" The number of count is " + count);
  
      // System.out.println(" The logical Size is " + logSize);
  
       }
     } 
  </code></pre>
  
  <p>binary search class:</p>
  
  <pre><code> public class BinSearch implements IBinarySearch {
  
   public int BinarySearch(int[] anArray, int logSize, int key) {
  
      int firstNum = 0;
      int lastNum = (anArray.length - 1);
      int middleNum = (firstNum + lastNum) / 2;
  
      while (firstNum &lt;= lastNum) {
          if (anArray[middleNum] &lt; key)
              firstNum = middleNum + 1;
          else if (anArray[middleNum] == key) {
              System.out.println(key + " found at location "
                      + (middleNum + 1) + ".");
              break;
          } else
              lastNum = middleNum - 1;
  
          middleNum = (firstNum + lastNum) / 2;
  
      }
  
      if (firstNum &gt; lastNum){
          System.out.println(key + " is not present in the list.\n");
      }
      return key;
     }
  
    }
  </code></pre>
  
  <p>output 1:</p>
  
  <pre><code> logSize 5134
   logSize 5743
   5134 found at location 329.
  </code></pre>
  
  <p>second output with a larger key:</p>
  
  <pre><code>logSize 9974
  logSize 9990
  9990 is not present in the list.
  </code></pre>