0

I've been trying to solve this Array out of bounds exception and have been stuck for a long time.

package mergesortmini;

public class MergeSortMini {
    public static void main(String[] args) {
        MergeSort m = new MergeSort();
        m.init().sort().printData();  
    }
}

class MergeSort {


   int[] data;


   public MergeSort init() {
      data = new int[10];
      data[0] = 5;
      data[1] = 67;
      data[2] = 0 - 12;
      data[3] = 7;
      data[4] = 1;
      data[5] = 2;
      data[6] = 99;
      data[7] = 3;
      data[8] = 4;
      data[9] = 67;
      return this;
   }


   public MergeSort sort() {
      data = this.mergeSort(data);
      return this;
   }

   public int printData() {
     int i = 0;  
     while(i < data.length){
         System.out.println(data[i]);
         i = i + 1;
     } 
     return data.length;
   }


   public int[] merge(int[] a1, int[] a2) {

       int lengthOfRes = a1.length + a2.length;
       int resArr[] = new int [lengthOfRes];    

       int a1Index = 0;
       int a2Index = 0;
       int resIndex = 0;

       while((a1Index < a1.length) || (a2Index < a2.length))
       {
           if((a1Index < a1.length) && (a2Index < a2.length)){

               if(a1[a1Index] < a2[a2Index]){
                   resArr[resIndex] = a1[a1Index];
                   a1Index = a1Index + 1;
                    resIndex = resIndex + 1;
               } else 
               {
                   resArr[resIndex] = a2[a2Index];
                   a2Index = a2Index + 1;
                   resIndex = resIndex + 1;
               }
           }
           else if(a1Index < a1.length){
                resArr[resIndex] = a1[a1Index];
                a1Index = a1Index + 1;
                resIndex = resIndex + 1;
           }
           else
           {
               resArr[resIndex] = a2[a2Index];
                a2Index = a2Index + 1;
                resIndex = resIndex + 1;
           }
       }
       return resArr;   
   }

   public int[] mergeSort(int[] arr) {

       if (arr.length < 2){
           return arr;  
       }
       int mid = arr.length / 2;  

       int leftArr[] = new int [mid];   
       int rightArr[];                 

        int halfLength = arr.length;
        if(isEven(halfLength))
        {
           rightArr = new int [mid];
        } 
        else 
        {
           rightArr = new int[mid + 1];
        }

       int resultArr[] = new int [arr.length];

       int i = 0;

       while(i < mid){  
           leftArr[i] = arr[i];
            i = i + 1;
       }
       int j = mid;
       int indexOfRight = 0;

       while(j < arr.length){
           rightArr[indexOfRight] = arr[j];
           indexOfRight = indexOfRight + 1; 
           j = j + 1;
       }

       leftArr = mergeSort(leftArr);
       rightArr = mergeSort(rightArr);

       return resultArr = merge(leftArr, rightArr);
   }

   public boolean isEven(int number){
        return (!((number - (number * 0.5) * 2) > 0)) && (!((number - (number * 0.5) * 2) < 0));
   }
}

The console displays:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at mergesortmini.MergeSort.mergeSort(MergeSortMini.java:118)
    at mergesortmini.MergeSort.mergeSort(MergeSortMini.java:123)
    at mergesortmini.MergeSort.sort(MergeSortMini.java:33)
    at mergesortmini.MergeSortMini.main(MergeSortMini.java:6)

If anyone has any suggestions, please help.

Oh just to mention my project specification declares I cannot use for loops, must use while, and I can only use a select number of operators.

The operators that I can use are:&& < + - *

frag r33f
  • 35
  • 4
  • pls mark line 118 in the source code with a comment – guido Feb 20 '15 at 00:11
  • 1
    How are you trying to solve it? With recursion I'd find a minimal example and track the variables involved to see what's happening. – keyser Feb 20 '15 at 00:12
  • The best approach here would be to step through the algorithm with a debugger. You already know which line is throwing the exception so just check the variables when you get to that line. – Brandon Feb 20 '15 at 00:20
  • I answered this same question in http://stackoverflow.com/questions/28612983/how-do-i-solve-this-stackoverflowerror/28613684#28613684 Please, don`t open duplicate entries and considers thank other members when they help you. – Marcelo Keiti Feb 20 '15 at 00:23
  • What is this I don't `isEven`...... – user253751 Feb 20 '15 at 00:24

1 Answers1

0

Your isEven method is broken: http://ideone.com/sMEQrP

You have to fix the fact that multiplying by a float will give a float (so it is not like an integer division):

public static boolean isEven(int number){
    return (!((number - (int)(number * 0.5) * 2) > 0)) 
        && (!((number - (int)(number * 0.5) * 2) < 0));
}
guido
  • 18,864
  • 6
  • 70
  • 95
  • @immibis he cannot use neither modulus, nor bitwise and – guido Feb 20 '15 at 00:26
  • Then look at what `isEven` is used for. `new int[arr.length - mid]` (and - *is* allowed) – user253751 Feb 20 '15 at 00:27
  • @immibis that's very true, but i was not code-revieweing here, i was trying to let him know the cause of his unexpected result – guido Feb 20 '15 at 00:30
  • @guido Yeah your right. But I Cannot use the division operator. When I chnage number into a double it still causes an error. I don't know what to do! – frag r33f Feb 20 '15 at 04:56
  • @fragr33f if you use my code above it works correctly, note the additional cast compare to your original method (while immibis solution in the comment above would be even better) – guido Feb 20 '15 at 09:52