I try to create a collection. Just a small collection with 3 names and i want to sort them by Alphabetically order. and it hits me always this error: Exception in thread "main" java.lang.AbstractMethodError: collection.compareTo(Ljava/lang/Object;)I. in the line: Collections.sort(names); what should i do to overcome my problem?
public class collection implements Comparable<collection> {
private String name;
public collection(String name){
this.name= name;
}
public String getName(){
return name;
}
public int compareΤο(collection c){
return this.getName().compareTo(c.getName());
}
}
public class collectionList {
private ArrayList <collection> names;
public collectionList(){
names = new ArrayList <collection>();
}
public void populate() {
collection c1 = new collection("Monica Rows");
names.add(c1);
collection c2 = new collection("Peter Walker");
names.add(c2);
collection c3 = new collection("Jack Miller");
names.add(c3);
}
public void sortBy(){
Collections.sort(names);
}
public String names(){
String s="";
for(collection c: names){
s+=c.getName()+ "\n";
}
return s;
}
}
public class collectionMain {
public static void main(String[] args){
collectionList c = new collectionList();
c.populate();
System.out.println(c.names());
c.sortBy();
System.out.println(c.names());
}
}
The name of your compareTo method is using unicode characters. The "T" is unicode 0x03A4 and the "o" is 0x03BF. They should be 0x0054 and 0x006F. You can check your characters using the following link.
http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=%26%23932%3B&mode=char
ignoring the style problems, you're compareTo method is using invalid characters:
compare%CE%A4%CE%BF
copy and paste it into an online url encoder http://meyerweb.com/eric/tools/dencoder/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With