title: Implementing Binary Search on Random Access Files in Java
body: <p>I am doing a program in Java where the user can create "databases" (.txt files) using random access files and store records there. I am working on implementing Binary Search in order to give the user the option to find a record by ID. </p>
  
  <pre><code> public static String binarySearch(String fileName, String id,String data_config) throws IOException 
      {
      RandomAccessFile Din = new RandomAccessFile(fileName, "r");
      num_records = getNumOfRecords(data_config);
      int Low = 0;
      int High = num_records;
      int Middle;
      String MiddleId;
      String record = "NOT_FOUND";
      boolean Found = false;
  
      while (!Found &amp;&amp; (High &gt;= Low)) 
      {
          Middle = (Low + High) / 2;
  
          record = getRecord(fileName,Middle,data_config);
          MiddleId = record.substring(0,3);
          int result = MiddleId.compareTo(id);
  
  
          if (result == 0)   // ids match
              Found = true;
          else if (result &lt; 0)
  
              Low = Middle + 1;
  
          else
  
              High = Middle -1;
  
      }
      return record;
  }
  </code></pre>
  
  <p>Here is the getRecord() method which works fine because I have tested it even without the binarySearch()method. </p>
  
  <pre><code>   public static String getRecord(String fileName, int recordNum,  String  data_config) throws IOException 
   {
      RandomAccessFile Din = new RandomAccessFile(fileName, "r");
      num_records = getNumOfRecords(data_config);
      String record = "NOT_FOUND";
      if ((recordNum &gt;=1) &amp;&amp; (recordNum &lt;= num_records))
      {
  
          Din.seek(0); // return to the top fo the file
          Din.skipBytes(recordNum * record_size);
          record = Din.readLine();
      }
  
      return record;
  }
  </code></pre>
  
  <p>The problem is at the compareTo()method used in binarySearch. It always returns -1, satisfying the second part of the if-else statement.
  For example, these are the records in one of my files:</p>
  
  <p>Id        Experience Married Wage         Industry<br>
  0001      1          no      123.0        kjasdhsjhjh<br>
  0002      1          yes     123.0        asdhajshjasdhja<br>
  0003      1          yes     124.0        ajskjkasjd<br>
  0004      1          yes     124.0        kasjdkjsdjs<br>
  0005      1          yes     124.0        kajskdjaksdjkas<br>
  0006      1          yes     123.0        kjksjdkasj   </p>
  
  <p>If I search for 0001:</p>
  
  <p>High = num_records  = 5;</p>
  
  <p>Low = 0, Therefore Middle =5/2 = 3</p>
  
  <p>It goes to the third record and it runs 0003 compareTo(0001). The result of this comparison is -1. Since it is less than 0, new Low is equal to Middle + 1 = 3+1 = 4, and it goes to check the fourth record even though it shouldn't. Since it is binary search, in this case it is supposed to check the second record because 0001 is less than 0003.</p>
  
  <p>Could you please help me find what I have done wrong here? </p>