Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Integer obj can't cast to Comparable
    primarykey
    data
    text
    <p>I'm having problems trying to pass an Integer object from a driver class as an argument for function of a SortedArray Generic class I created. From my driver class, I convert the user's int input into an Integer object to be cast onto Comparable of my SortedArray class.</p> <p>I continue to receive the error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable". I took a look at some of my classmates' source codes only to find little difference in setting the arguments/parameters yet they have their code working just fine. I've been looking for hours trying to find what error I've made and I still can't find why my Integer object can't be cast to Comparable.</p> <p>Here's a bit from my SortedArray Class</p> <pre><code>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 &lt;= top; i++) { if(a[i].compareTo(value) &gt; 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&lt; size; i++) { tempArray[i]= a[i]; a = tempArray; } size = top + increment; } if(a[appropriatePosition(value) + 1] != null) { for(int i = top; i &lt; appropriatePosition(value); i--) { a[i + 1] = a[i]; } } a[appropriatePosition(value) + 1]= value; } </code></pre> <p>Here's the code for my driver class that passes Integer Object insertObj as an argument for SortedArray's insert function.</p> <pre><code>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's where I lay "insertObj" as an argument for the SortedArray function. System.out.println("The value " + data + " is inserted"); break; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload