title: StackOverflowError in recursive binary search in file in java
body: <p>The <code>filepointer</code> gets stuck at a point resulting in a <code>StackOverflowError</code>. Can you point me to what exactly is wrong here? The error exactly is: <code>java.lang.StackOverflowError</code></p>
  
  <p>I am doing a seek to find the location as record width is not fixed.</p>
  
  <p>Here is the piece of code:</p>
  
  <pre><code>private static void binarySearch(RandomAccessFile raf, String searchvalue, Long low, Long high) throws IOException
  {
      Long middle = (low + high) / 2;
      Long mreal = null;
      if(low &gt; raf.length() -1 || high &gt; raf.length()-1 || low &gt;= high) {
          System.out.println("Element not found:");   return ;
      }
  
      StringBuilder sb = new StringBuilder();
      for(long filePointer = middle; filePointer != -1; filePointer--) {
          raf.seek(filePointer);
          int readByte = raf.readByte();
          if(readByte == 0xA) {
              break;
          }
  
          sb.append((char)readByte);
      }
  
      String lastLine = sb.reverse().toString();
      System.out.println(lastLine);
  
      mreal = raf.getFilePointer();
      String str = raf.readLine();
      System.out.println(str);
  
      String values[] = str.split("\t",-1);
      int compared = searchvalue.compareTo(values[fieldindex]);
      System.out.println(fieldindex);
  
      if(compared == 0) {
          System.out.println("Value found. The other details:");
          for(int i=0; i &lt; values.length;i++)
          System.out.print("\t" +  values[i]);
          return;
      } else if(compared &lt; 0)
          binarySearch(raf,searchvalue,low,mreal-1);
      else if(compared &gt; 0)
          binarySearch(raf,searchvalue,mreal,high);
  }
  </code></pre>
  
  <p>Stack trace:</p>
  
  <p>Exception in thread "main" java.lang.StackOverflowError</p>
  
  <pre><code>&gt;at java.util.regex.Pattern$Node.&lt;init&gt;(Pattern.java:2993)
      &gt;at java.util.regex.Pattern$CharProperty.&lt;init&gt;(Pattern.java:3332)
      &gt;at java.util.regex.Pattern$CharProperty.&lt;init&gt;(Pattern.java:3332)
      &gt;at java.util.regex.Pattern$BmpCharProperty.&lt;init&gt;(Pattern.java:3363)
      &gt;at java.util.regex.Pattern$BmpCharProperty.&lt;init&gt;(Pattern.java:3363)
      &gt;at java.util.regex.Pattern$Single.&lt;init&gt;(Pattern.java:3391)
      &gt;at java.util.regex.Pattern.newSingle(Pattern.java:2951)
      &gt;at java.util.regex.Pattern.atom(Pattern.java:1985)
      &gt;at java.util.regex.Pattern.sequence(Pattern.java:1885)
      &gt;at java.util.regex.Pattern.expr(Pattern.java:1752)
      &gt;at java.util.regex.Pattern.compile(Pattern.java:1460)
      &gt;at java.util.regex.Pattern.&lt;init&gt;(Pattern.java:1133)
      &gt;at java.util.regex.Pattern.compile(Pattern.java:823)
      &gt;at java.lang.String.split(String.java:2292)
  </code></pre>