title: Binary Search problem - Help - Trying for hours
body: <p>I hope you do not mind me posting this up.</p>
  <p>I have a problem, and I have been looking at other examples of code from StackOverflow and I still cannot key the correct location on the value I am looking for.</p>
  <p>I am to:</p>
  <p>//Description: Performs a strong text binary search on an array for a specified value
  //Parameters: int array of values and int key which item to be searched
  //Returns: int indicating the first location of the item, or -1 in the case
  //the key is not found.</p>
  <p>It would be greatly appreciated if the simplest and most efficient way of doing this can be solved. Thank you in advance!</p>
  <p>My code so far:</p>
  <pre><code>import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  
  public class BinarySearching
  
  {
  public static int binarySearch(int[] array, int key)
  {
  
    int begin = 0;
    int last = array.length -1;
    int mid =0;
  
    while (begin &lt;= last)
    {
       mid = (begin + last) / 2;
  
       // Check if key is present at mid
       if (array[mid] &lt; key)
       {
          begin = mid +1;
       }
  
       // If key greater, ignore left half
       else if (array[mid] &gt; key)
       {
          last = mid - 1;
       }
          // If x is smaller, ignore right half
       else
       {
          return mid;
       }
    }
  
    // if we reach here, then element was
    // not present
  
    if(last == 0)
    {
       return -1;
    }
  
    return mid;
  }
  
  public static void main(String[] args)
  {
  
    int[] numbers = readArrayFromFile(&quot;practice_data.txt&quot;);
  
  
    int key = 3555318 ;
  
    int result = binarySearch(numbers, key);
  
    if (result == -1)
    {
       System.out.println(&quot;Element not present &quot; +result);
    }
    else
    {
       System.out.println(&quot;Element found at &quot;
             + &quot;index &quot; + result);
    }
  
  }//main
  }//class
  </code></pre>
  <p>/////////////////////////////////////////////////////////////////////////////////</p>
  <p>Sample of text file. This would on my intellj be going to the next line after each number is entered.</p>
  <p>Example:
  1
  2
  3
  4</p>
  <p>Sample from Text file:</p>
  <p>10000
  -6335552
  2276239
  -9496279
  -2570465
  -3131620
  6185632
  6080225
  -8641474
  802221
  4654808
  -6425849
  1001941
  -1884575
  3555318
  3555318
  3555318
  2280363
  -5578898
  -1843525
  2106946
  1142271
  5735601
  6551052
  -1367885
  3972930
  -5147855
  6901888
  9999313
  -443147
  5700530
  9439329
  -4407250
  8461347</p>