I always get the NumberFormatException error from the following simple program called TestSort.java. The error came from where I tried to convert a
string into a Long object. Can anyone know what the problem is? Thanks.
TestSort.java:
import java.util.*;
public class TestSort implements Comparator {
public int compare (Object obj1, Object obj2) {
int i, j, k;
String str1 = (String) obj1;
String str2 = (String) obj2;
i = str1.lastIndexOf(' ');
j = str2.lastIndexOf (' ');
Long li = Long.valueOf(str1.substring (i));
Long lj = Long.valueOf(str2.substring (j));
k = li.compareTo(lj);
return k;
}
public static void main (String args[]) {
TreeSet ts = new TreeSet (new TestSort());
ts.add ("aaa 9000000000");
ts.add ("bbb 7000000000");
ts.add ("ccc 8000000000");
System.out.println ("ts contains " + ts);
}
}