1

I'm using Eclipse and it's giving me an error saying "Cannot invoke compareTo(int) on the primitive type int". Here is my code:

public class ReadingMaterial implements Comparable<ReadingMaterial> {

    private int pages;
    private String title;
    private String author;


    public ReadingMaterial(int pages, String title, String author) 
    {
        super();
        this.title = title;
        this.pages = pages;
        this.author = author;

    }

    public int getPages() 
    {
        return pages;
    }

    public void setPages(int pages) 
    {
        this.pages = pages;
    }


    public String getAuthor() 
    {
        return author;
    }

    public void setAuthor(String author) 
    {
        this.author = author;
    }

    public String getTitle() 
    {
        return title;
    }

    public void setTitle(String title) 
    {
        this.title = title;
    }

    @Override
    public int compareTo(ReadingMaterial o) {

        if(pages.compareTo(o.getPages()) > 0)
            return -1;
        else if(pages.compareTo(o.getPages()) < 0)
            return 1;
        else
        {
            return 0;
        }
    }

    @Override
    public String toString() 
    {
        return "The reading material you selected is titled " + title + " with" + pages + "pages. It is written by " + author + ".";

    }

}

The example we were given in class does a similar thing except it is using a double value, so I'm not sure why the file from class works, but mine doesn't. I'd appreciate any help.

Example from class:

import java.text.DecimalFormat;
public class Employee implements Comparable<Employee> {
    private Double salary;
    private int hoursWorked;
    private String firstName;
    private String lastName;
    private static DecimalFormat df = new DecimalFormat("0.00");
    public Employee(double salary, int hoursWorked, String firstName,
            String lastName) {
        this.salary = salary;
        this.hoursWorked = hoursWorked;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    @Override
    public String toString() {
        return firstName + " " + 
                lastName + " works " + 
                hoursWorked + " hours and makes $" + 
                df.format(salary);
    }
    public Double getSalary() {
        return salary;
    }
    public void setSalary(Double salary) {
        this.salary = salary;
    }
    public int getHoursWorked() {
        return hoursWorked;
    }
    public void setHoursWorked(int hoursWorked) {
        this.hoursWorked = hoursWorked;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    @Override
    public int compareTo(Employee o) {
        // TODO Auto-generated method stub
        if(salary.compareTo(o.getSalary()) > 0)
            return -1;
        else if(salary.compareTo(o.getSalary()) < 0)
            return 1;
        else
        {
            return lastName.compareTo(o.getLastName());
        }
    }

}
ViviO
  • 43
  • 1
  • 8
  • The example from the class is using a `Double`, not a `double`. There's a huge difference between a primitive type and its corresponding wrapper class. Hopefully, there exists utility methods available such as `Integer.compare(int x, int y);` – Alexis C. Feb 12 '16 at 21:21
  • 4
    Use `Integer.compare(pages, o.pages)` instead of trying to call a method on a primitive. – Hovercraft Full Of Eels Feb 12 '16 at 21:22
  • the compareTo method is usually invoked on an object. int is a primitive in Java while its equivalent Object is Integer. Usually auto-boxing and unboxing takes care of converting between primitive and its object equivalent. You can solve the issue by using `Integer.valueOf(pages).compareTo(o.getPages)`. Also, all this could be done in one return call – chaitanya Feb 12 '16 at 21:26
  • when calculations are involved, try to avoid using double. You can use java.math.BigDecimal. Once you use BigDecimal, comparison with other value is easier and accurate as well – Rakesh Feb 12 '16 at 21:35
  • Ah thank you for the tip about the difference between Double and double. I didn't notice that when looking at the example. Thanks Hovercraft Full of Eels - my professor wanted us to use the compareTo method for this particular problem, so I was trying to go by his example in class. But I'll keep that in mind for future problems. :) – ViviO Feb 12 '16 at 21:38
  • Oh, I didn't think about using the Integer.valueOf(pages) part. Hmm.. Interesting. Thanks chaitanya! – ViviO Feb 12 '16 at 21:39
  • @Rakesh - Ohh, I didn't know about BigDecimal. Honestly, I'm pretty new to Java. Just in the second class at my college. Thanks :) – ViviO Feb 12 '16 at 21:41
  • If you feel answer is satisfactory, please mark it correct. – Rakesh Feb 12 '16 at 21:48

0 Answers0