Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I need Y the ClaaCastException is coming?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
the following program is generating classcastexcception.I need the reason for that and also i need how to catch that exception

import java.util.*;
public class Test1{
public static void main(String a[]) {
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note the error message...

This tells you that the problem starts at line 6 of your Test1.java file, which is when you try to add the second element. If you check the API documentation for TreeSet, you will see that the add method throws a ClassCastException "if the specified object cannot be compared with the elements currently in the set."

Elements in a TreeSet are "sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used."

So the problem is that these elements (instances of Person) are not Comparable and no Comparator is provided to the set constructor.
[ April 13, 2006: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajitha Murthy:
...and also i need how to catch that exception...


In general, you catch exceptions by placing the code that might throw an exception inside a try block, and then follow the try block with a catch block that "handles" the exception...

There's a bit more to it, but that's a general idea of how to catch an exception.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,i made some changes to your code and this is working now.You have override the method compareTo.....
Have a look at the below code that will give you better understanding.

import java.util.*;

public class Test1 {
public static void main(String a[]) {
try {
Set s = new TreeSet();
s.add(new Person(10));
s.add(new Person(20));
System.out.println(s);
} catch (ClassCastException e) {
e.printStackTrace();
}
}

}


class Person implements Comparable{
int j;
Person(int i) {
j=i;
}
public int compareTo(Object o) {
if((o instanceof Person)&&(((Person)o).j ==this.j))
return 0;
return 1;
}
}
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by subir rastogi:
hi,i made some changes to your code and this is working now.You have override the method compareTo...


Actually, compareTo should return a negative value if this object is "less than" the object it's being compared to; zero if they are equal, and a positive value if this object is "greater than" the object it's being compared to.

In this case, you could simply return the difference of Person's int value...

Also from the API for Comparator...

It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals.


And finally, you will probably want to override the toSting method in Person, so that when you print your TreeSet you can verify the ordering.
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic