Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Cannot Cast Object to Date in Comparator
    primarykey
    data
    text
    <p>I am attempting to sort a collection of Dates in LinkedList myQueue by emptying it into ArrayList myArrayList, sorting it via Comparator, then iterating over the linked list to place each Date back into the myQueue. </p> <p>Everything works fine until I send the Date Objects into My Comparator for some reasin Casting the dates I created into Dates in the Comparator does not work.</p> <p>Here is the Format the Dates are in:</p> <pre><code> Thu Jul 30 00:00:00 JST 1908 </code></pre> <p>Here is the Error:</p> <pre><code> Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to Date at DateComparator.compare(DateComparator.java:14) at java.util.TimSort.countRunAndMakeAscending(Unknown Source) at java.util.TimSort.sort(Unknown Source) at java.util.TimSort.sort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at java.util.Collections.sort(Unknown Source) at QueueTest.main(QueueTest.java:76) </code></pre> <p>This is Where I place all Objects in myQueue into myArrayList:</p> <pre><code> if (!myQueue.isEmpty()) { //Collections.sort(myQueue, new DateComparator()); Object nextDate; ArrayList myArrayList = new ArrayList(); while(!myQueue.isEmpty()){ nextDate = myQueue.removeFirst(); myArrayList.add(nextDate); } Collections.sort(myArrayList, new DateComparator()); Iterator it=myArrayList.iterator(); while(it.hasNext()){ myQueue.addLast(it.next()); } </code></pre> <p>And this is where the issue is in My Comparator:</p> <pre><code> Date d1 = (Date) a; Date d2 = (Date) b; </code></pre> <p>I just started going to school for Computer Science in Okinawa, got nobody to really talk to about this asides from my dog so any help is appreciated.</p> <p>//EDIT:</p> <p>This is the Date() SuperClass that My professor requires we use in order to create out Date Objects:</p> <pre><code>public class Date { private static int numberDates=0; public static final int VALID_START_YEAR=1584; private int year; private int month; private int day; public Date() { month = 1; day = 1; year = 1970; numberDates++; } public Date(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; numberDates++; } public Date( Date other) { month = other.month; day = other.day; year = other.year; numberDates++; } public Date( String date) { java.util.StringTokenizer st = new java.util.StringTokenizer(date,"/"); month = Integer.parseInt(st.nextToken()); //extract next token day = Integer.parseInt(st.nextToken()); year = Integer.parseInt(st.nextToken()); numberDates++; } public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public String toString() { return(month + "/" + day + "/" + year); } public static int getNumberDates() { return numberDates; } public void increment() { day++; //stub for the actual method to be written later... } public static void clearNumberDates() { numberDates = 0; } public static boolean isValidDay(int month, int day, int year) { return true; } public boolean equals( Object other ) { if ((other instanceof Date) &amp;&amp; ((((Date)other).year==year) &amp;&amp; (((Date)other).month==month) &amp;&amp; (((Date)other).day==day))) return true; else return false; } public int hashCode() { return (year + month + day) % 101; } public int compareTo( Date other ) { if (equals(other)) return 0; else if (year &lt; other.year) return -1; else if (year &gt; other.year) return 1; else if (month &lt; other.month) return -1; else if (month &gt; other.month) return 1; else if (day &lt; other.day) return -1; else if (day &gt; other.day) return 1; else return 0; } </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.
    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