Question

What's wrong with this code? #Java Why I am getting error in : int BusCompare =...

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);
    }
}

Homework Answers

Answer #1

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 :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
In Java Let’s say we’re designing a Student class that has two data fields. One data...
In Java Let’s say we’re designing a Student class that has two data fields. One data field, called id, is for storing each student’s ID number. Another data field, called numStudents, keeps track of how many Student objects have been created. For each of the blanks, indicate the appropriate choice. id should be   public/private   and   static/not static . numStudents should be   public/private   and   static/not static . The next three questions use the following class: class Counter {     private int...
I am not sure what I am doing wrong in this coding. I keep getting this...
I am not sure what I am doing wrong in this coding. I keep getting this error. I tried to change it to Circle2D.java but it still comes an error: Compiler error: class Circle2D is public, should be declared in a file named Circle2D.java public class Circle2D { public class Exercise10_11 { public static void main(String[] args) {     Circle2D c1 = new Circle2D(2, 2, 5.5);     System.out.println("area: " + c1.getArea());     System.out.println("perimeter: " + c1.getPerimeter());     System.out.println("contains(3, 3): "...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
How do I get this portion of my List Iterator code to work? This is a...
How do I get this portion of my List Iterator code to work? This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this Exception in thread "main" java.lang.NullPointerException    at AddressBookApp.main(AddressBookApp.java:36) iter.reset(); Person findPerson = iter.findLastname("Duck"); if (findPerson == null) System.out.println("Person not found."); else findPerson.displayEntry(); findPerson = iter.findLastname("Duck"); if (findPerson == null) { System.out.println("Person not found.");...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT