Java: Integer obj cannot pass to Comparable

I'm having trouble trying to pass an Integer object from a driver class as an argument to a function of the SortedArray Generic class that I created. From my driver class, I convert the int user input into an Integer object that needs to be overlaid on the Comparable of my SortedArray class.

I keep getting the error: "Exception in thread" main "java.lang.ClassCastException: java.lang.Integer cannot be passed to Comparable." I looked at some of my sources just to find a slight difference in the settings / parameters, but they have very good code. I searched the clock, trying to find what mistake I made, and I still can not find why my Integer object cannot be passed to Comparable.

Here is a bit of my SortedArray class

public class SortedArray implements Comparable{
    public int size;
public int increment;
public int top;
    Comparable[] a = new Comparable [size];

public SortedArray(int initialSize, int incrementAmount)
{
        top = -1;
        size = initialSize;
        increment = incrementAmount;

}
public int appropriatePosition(Comparable value)
{
        int hold = 0;
        if(top == -1)
        {
            return 0;
        }
        else
        {    
            for(int i = 0; i <= top; i++)
            {
                if(a[i].compareTo(value) > 0)
                {
                    hold = i;
                    break;
                }
            }
        }
        return hold;
}

public void insert(Comparable value) //The function that my driver class needs to access
{
        //Shifting numbers to the top
        if(full() == true)
        {
            Comparable[] tempArray = new Comparable[top + increment];
            for(int i= 0; i< size; i++)
            {
                tempArray[i]= a[i];
                a  = tempArray;
            }
            size = top + increment;
        }
        if(a[appropriatePosition(value) + 1] != null)
        {
            for(int i = top; i < appropriatePosition(value); i--)
            {
                a[i + 1] = a[i];
            }
        }
        a[appropriatePosition(value) + 1]= value;
    }

Here is the code for my driver class that passes the Integer Object insertObj as an argument to the SortedArray insert function.

public class IntDriver  {
 public static void main(String[] args)
 {
     Scanner keyboard = new Scanner(System.in);
     //Creating variables
     int data;
     boolean check = false;
     int choice;
     int size = 5;
     int increment = 3;
     SortedArray b = new SortedArray(size, increment);
     //Creating Menu
     while(check == false)
     {
     System.out.println("Please choose through options 1-6.");
     System.out.println("1. Insert\n2. Delete\n3. Clear\n4. Smallest\n5. Largest\n6. Exit");
     choice = keyboard.nextInt();
     switch(choice)
         {
         case 1:
             System.out.println("Type the int data to store in array location.");
             data = keyboard.nextInt();
             Integer insertObj = new Integer(data);
             b.insert(insertObj);// Here where I lay "insertObj" as an argument for the SortedArray function.
             System.out.println("The value " + data + " is inserted");
            break;
+3
source share
3 answers

The problem is that Integerextends java.lang.Comparable, then yours is Comparablenot java.lang.Comparable. Look at the error message, your Comparablecomes from the default package, and not java.lang:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable

That is, you cannot use java.lang.Integerfor your class

+5
source

As axtavt mentioned , the problem is that you have your own Comparable class. More specifically, this means that:

Integer.valueOf(1) instanceof java.util.Comparable == true
Integer.valueOf(1) instanceof Comparable == false

This means that somewhere in your code there is something like:

Object[] a = new Object[] {Integer.valueOf(1);};
Comparable x = (Comparable) a[0];
// or something equivalent, this is likely being passed through layers
// and not being done next to each other like this.

:

Object[] a = new Object[] {Integer.valueOf(1);};
java.util.Comparable x = (java.util.Comparable) a[0];

, Comparator , Java. , Java , , , , .

+4
0

Source: https://habr.com/ru/post/1729447/


All Articles