What's wrong with this code? #Java
Why I am getting error in : int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses);
public class TypeComparator implements Comparator<Type> { @Override public int compare(Type o1, Type o2) { // return o1.name.compareTo(o2.name); int NameCompare = o1.name.compareTo(o2.name); int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); // 2-level comparison using if-else block if (NameCompare == 0) { return ((BusCompare == 0) ? NameCompare : BusCompare); } else { return NameCompare; } } }
public class Type implements Comparable<Type> { int id; String name; int numberOfBusinesses; public Type(int id, String name, int numberOfBusinesses) { this.id = id; this.name = name; this.numberOfBusinesses = numberOfBusinesses; } @Override public String toString() { // Prints the information for each Chain object // you can use it for testing your solution return "[Chain with id = " + this.id + ", name = " + this.name + ", n businesses " + this.numberOfBusinesses + "]"; } @Override public int compareTo(Type o) { return id - o.id; } }
public class Main { public static void main(String[] args) { // write your code here Type c1 = new Type(10, "Target", 1844); Type c2 = new Type(2, "Whole Foods", 480); Type c3 = new Type(3, "Whole Foods", 500); List<Type> arr = new ArrayList<>(); arr.add(c1); arr.add(c2); arr.add(c3); Collections.sort(arr, new TypeComparator()); // System.out.println(c2.equals(c3)); System.out.println(arr); } }
You can change the line to the :
int BusCompare = String.valueOf(o1.numberOfBusinesses).compareTo(String.valueOf(o2.numberOfBusinesses));
Now the error can be completely gone
Modified Code :
import java.util.*;
class TypeComparator implements Comparator<Type> {
@Override
public int compare(Type o1, Type o2) {
// return o1.name.compareTo(o2.name);
int NameCompare = o1.name.compareTo(o2.name);
int BusCompare =
String.valueOf(o1.numberOfBusinesses).compareTo(String.valueOf(o2.numberOfBusinesses));
// changed line
// 2-level comparison using if-else block
if (NameCompare == 0) {
return ((BusCompare == 0) ? NameCompare : BusCompare);
} else {
return NameCompare;
}
}
}
class Type implements Comparable<Type> {
int id;
String name;
int numberOfBusinesses;
public Type(int id, String name, int numberOfBusinesses) {
this.id = id;
this.name = name;
this.numberOfBusinesses = numberOfBusinesses;
}
@Override
public String toString() {
// Prints the information for each Chain object
// you can use it for testing your solution
return "[Chain with id = " + this.id + ", name = " + this.name
+
", n businesses " + this.numberOfBusinesses + "]";
}
@Override
public int compareTo(Type o) {
return id - o.id;
}
}
public class Main {
public static void main(String[] args) {
// write your code here
Type c1 = new Type(10, "Target", 1844);
Type c2 = new Type(2, "Whole Foods", 480);
Type c3 = new Type(3, "Whole Foods", 500);
List<Type> arr = new ArrayList<>();
arr.add(c1);
arr.add(c2);
arr.add(c3);
Collections.sort(arr, new TypeComparator());
// System.out.println(c2.equals(c3));
System.out.println(arr);
}
}
OUTPUT :
Get Answers For Free
Most questions answered within 1 hours.