0

After running a program, it directed me to If (key.compareTo(a[mid]) == 0) What's wrong with it?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at BinarySearch.search(BinarySearch.java:17) at SearchesDemo.main(SearchesDemo.java:23)

import java.util.Scanner;
import java.util.Arrays;

public class SearchesDemo{

  public static void main(String[] args){
    BinarySearch searches = new BinarySearch();
    int result, key;
    Integer [] integerArray = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18};
    String [] stringArray = {"apples", "oranges", "peaches", "strawberries", "watermelons"};

    System.out.println("Integer test array contains:");
    System.out.println(Arrays.toString(integerArray));

    for(key = -3; key == 4; key++){
      result = searches.<Integer>search(integerArray, 0, 10, key);
      searches.toString(Integer.toString(key), result);
    }

    System.out.println("\nString test array contains:");
    System.out.println(Arrays.toString(stringArray)); //toString may not be necessary

    result = searches.<String>search(stringArray, 0, 10, "apples");
    searches.toString("apples", result);
    result = searches.<String>search(stringArray, 0, 10, "plums");
    searches.toString("plums", result);

    System.out.println("\nProcess completed.");
  }
}

public class BinarySearch{


  public static <T extends Comparable> int search(T [] a, int first, int last, T key){
    int result = 0; //to keep the compiler happy.

    if (first > last)
      result = -1;
    else{
      int mid = (first + last)/2;

      if (key.compareTo(a[mid]) == 0)
        result = mid;
      else if (key.compareTo(a[mid]) < 0)
        result = search(a, first, mid - 1, key);
      else if (key.compareTo(a[mid]) > 0)
        result = search(a, mid + 1, last, key);
      else{
        System.out.println("Error");
        result = -1;
      }
    }
    return result;
  }

  public static void toString(String key, int result){
    if(result == -1)
      System.out.println(key + " is not in the array.");
    else
      System.out.println(key + " is at index " + result);
  }
}
mustaccio
  • 18,234
  • 16
  • 48
  • 57
blacklune
  • 43
  • 1
  • 7
  • Your evaluation of the mid point of 0 and 10 gives you 5, but to access the fifth element of the array, you would need a[4], as it starts at 0 index. It's a 5 length array of strings hence (0-4). – ManoDestra Mar 22 '16 at 20:43

3 Answers3

1

Dont send 10 for the last value. Just send the length of your array.

result = searches.<String>search(stringArray, 0, stringArray.length, "apples");
searches.toString("apples", result);
result = searches.<String>search(stringArray, 0, stringArray.length, "plums");
searches.toString("plums", result);

Same in here:

result = searches.<Integer>search(integerArray, 0, integerArray.length, key);
rdonuk
  • 3,921
  • 21
  • 39
1

The problem is that you have 5 elements in your stringArray (index 0, 1, 2, 3, 4), and since you start with begining index at 0 and last at 10, the medium is 5 and thus you try to access an index not there -> out of bound.

Jonathan Schoreels
  • 1,660
  • 1
  • 12
  • 20
  • Sorry but I see "SearchesDemo.main(SearchesDemo.java:23)" which is this line : " result = searches.search(stringArray, 0, 10, "apples");" – Jonathan Schoreels Mar 22 '16 at 20:44
  • @FallAndLearn - nope. Look at the code and exception again. It states the line no. 23. The author of the question unfortunately placed content of the two files in the same code block. The exception points to the class `SearchesDemo` to line 23 (`result = searches.search(stringArray, 0, 10, "apples");`). – Tomasz Dzieniak Mar 22 '16 at 20:44
  • Sorry my fault. I saw the other way round :) – FallAndLearn Mar 22 '16 at 20:45
1

The exception occurs in line number 23 in main method. You are passing start and end indexes as 0 and 10.

 result = searches.<String>search(stringArray, 0, 10, "apples");

Your stringarray has length only 5. When mid is calculated it becomes (0+10)/2 which is 5. arr[5] throws arrayIndexOutOfBound exception.

FallAndLearn
  • 4,035
  • 1
  • 18
  • 24