title: BinaryRecursion Error message: StackOverflowError
body: <p>When I run the below program I am getting the following errors: (any ideas?)</p>
  
  <pre><code>Exception in thread "main" java.lang.StackOverflowError
  at SumArray.binarySum(SumArray.java:31)
  at SumArray.binarySum(SumArray.java:34)
  </code></pre>
  
  <hr>
  
  <p>Here is the code: I need to generate an array of random integers and use binary and linear recursion to add the values thereof... </p>
  
  <pre><code>import java.io.*;
  import java.util.ArrayList;
  import java.util.Random;
  
  class SumArray {
      static int floor = 100000;
      static int ceil = 0; 
      int result;
      int half; 
      int half2;
  
      //Linear Recursion
      int sum(int a[], int n) {
          if (n == 1)
              return a[0];
          else {
              result = sum(a, n - 1);
              result = result + a[n - 1];
              return result;
          }
      }
  
      //Binary Recursion
      int binarySum(int a[], int i, int n) {
          if (n == 1)
              return a[0];
  
          return binarySum(a, i, ceil/2) + binarySum(a, i + ceil/2, floor/2);
      }
  
      public static void main(String args[]) throws IOException {
  
          //Define ArrayList to hold Integer objects
          ArrayList&lt;Integer&gt; numbers = new ArrayList&lt;Integer&gt;();
  
          // User determines number of elements
          DataInputStream dis = new DataInputStream(System.in);
          System.out.println("Hello! Please enter the size of your array: ");
          int n = Integer.parseInt(dis.readLine());
  
          // Generate specified number of random integers
          Random randNumGenerator = new Random();
          int[] x = new int[n];
  
          // While still less than indicated number.. add more
          for (int i = 0; i &lt; x.length; i++) {
              x[i] = (randNumGenerator.nextInt(100000));
              numbers.add(x[i]);
  
              //calculate min and max values
              if(ceil &lt; x[i]) {
                  ceil = x[i];
              }
              if(floor &gt; x[i]) {
                  floor = x[i];
              }
          }
  
          SumArray a1 = new SumArray();
  
          // Print array values
          System.out.println("Array values inlude: " + numbers);
  
          // Print the sum
          System.out.println("The sum of the array using Linear Recursion is: " + a1.sum(x, n));
  
          // Print the binary sum
          System.out.println("The sum of the array using Binary Recursion is: " + a1.binarySum(x, n, n));
  
          System.out.println("Ceiling: " + ceil);
          System.out.println("Floor: " + floor);
      }
  }
  </code></pre>