title: Binary Search CompareTo Java
body: <p>I'm trying to use a binary search on an Object Array. I'm using object because for one instance I may have a set of strings or <code>ints</code>. I'm currently stuck at implementing my <code>compareTo</code> method, not really sure of what the next step would be. 
  Here is what I have so far -  </p>
  
  <pre><code>public static int binarySearch(Object[] items, Comparable target, int first, int last){
  
      if(first &gt; last)
          return -1; // Base case for unsuccessful search
      else{
          int middle = (first + last) / 2; // Next probe index.
          int compResult = target.compareTo(items[middle]);
          if(compResult == 0)
              return middle; // Base case for unsuccessful search.
          else if (compResult &lt;0)
              return binarySearch(items, target, first, middle -1);
          else
              return binarySearch(items, target, middle + 1, last);
      }
  }
  public static int binarySearch(Object[] items, Comparable target){
      return binarySearch(items, target, 0, items.length -1);
  }
  @Override
  public int compareTo(T obj) {
  
      return 0;
  }
  public static void main(String[] args){
  String[] names = {"Caryn", "Debbie", "Dustin", "Elliot", "Jacquie", "Jonathan", "Rich"};
  
      int myName = binarySearch(names, "Dustin");
  </code></pre>
  
  <p>I'm getting an error when I call binarySearch it says The method binarySearch(Object[], Comparable) in the type FiveThree is not applicable for the arguments (String[], String). I know its because my <code>CompareTo</code> is empty right now, but I don't know how to make "Dustin" or whatever parameter I put second a Comparable rather than string. Also if I cast object in front of names it only recognizes it only as an Object not an Object[].<br>
  Thanks.</p>