Hi,

I am getting an Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol error message when I try to run this merge/sort program. I can't figure out how to fix the program to get rid of the error. Please help.

import java.util.Random;

public class ArrayUtil
{
  
   public static int[] randomIntArray(int length, int n)
   {
      int[] a = new int[length];
      for (int i = 0; i < a.length; i++)
         a[i] = generator.nextInt(n);

      return a;
   }

   private static Random generator = new Random();
}
public class MergeSorter
{
   public MergeSorter(int[] anArray)
   {
      a = anArray;
      
   }

   /**
      Sorts the array managed by this merge sorter
   */
   public void sort()
         throws InterruptedException
   {
      mergeSort(0, a.length - 1);
   }

   /**
      Sorts a range of the array, using the merge sort
      algorithm.
      @param from the first index of the range to sort
      @param to the last index of the range to sort
   */
   public void mergeSort(int from, int to)
         throws InterruptedException
   {
      if (from == to) return;
      int mid = (from + to) / 2;
       // sort the first and the second half
      mergeSort(from, mid);
      mergeSort(mid + 1, to);
      merge(from, mid, to);
   }

   /**
      Merges two adjacent subranges of the array
      @param from the index of the first element of the first range
      @param mid the index of the last element of the first range
      @param to the index of the last element of the second range
   */
   public void merge(int from, int mid, int to)
         throws InterruptedException
   {
      startPosition = from;
      endPosition = to;

      int n = to - from + 1;
      // size of the range to be merged

      // merge both halves into a temporary array b
      int[] b = new int[n];

      int i1 = from;
      // next element to consider in the first range
      int i2 = mid + 1;
      // next element to consider in the second range
      int j = 0;
      // next open position in b

      // as long as neither i1 nor i2 past the end, move
      // the smaller element into b
      while (i1 <= mid && i2 <= to)
      {
         if (a[i1] < a[i2])
         {
            b[j] = a[i1];
            markedPosition = i1;
            i1++;
         }
         else
         {
            b[j] = a[i2];
            markedPosition = i2;
            i2++;
         }

         pause(4);
         j++;
      }

      // note that only one of the two while loops
      // below is executed

      // copy any remaining entries of the first half
      while (i1 <= mid)
      {
         b[j] = a[i1];
         markedPosition = i1;
         pause(2);
         i1++;
         j++;
      }

      // copy any remaining entries of the second half
      while (i2 <= to)
      {
         b[j] = a[i2];
         markedPosition = i2;
         pause(2);
         i2++;
         j++;
      }

      // copy back from the temporary array
      for (j = 0; j < n; j++)
      {
         a[from + j] = b[j];
         markedPosition = from + j;
         pause(2);
      }
   }


   public void pause(int steps)
         throws InterruptedException
   {
      if (Thread.interrupted())
         throw new InterruptedException();
      
      Thread.sleep(steps * DELAY);
   }

   private int[] a;

   private int markedPosition = -1;
   private int startPosition = -1;
   private int endPosition = -1;

   
   private static final int DELAY = 100;
}
public class MergeSortDemo
{
   public static void main(String[] args)
   {
      int[] a = ArrayUtil.randomIntArray(2000, 10000);
      System.out.println(Arrays.toString(a));

      MergeSorter sorter = new MergeSorter(a);
      Thread t = new Thread(sorter);
      try
      {
         t.start();
         t.join();
      }
      catch (InterruptedException ex)
      {
      }
      System.out.println(Arrays.toString(a));
   }
}

The error is occurring at "Thread t = new Thread(sorter);". Here is the full error message...

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: constructor Thread(merge_sorter_20_7.MergeSorter)
location: class java.lang.Thread

Thanks in advance for any help you can give me with this.

Read the API for Thread's constructor - your parameter should be an object that implements Runnable, which MergeSorter doesn't.

Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.