"Cannot invoke compareTo(int) on the primitive type int"
You have to understand: the primitive type int isn't a reference type like Integer. You can't call methods on primitive values. When you want to compare two ints, you can simply do index < newNumber for example.
Alternatively, you could use that static method compare of the Integer class, like: Integer.compare(index, newValue). But as said: you don't need methods to compare primitive values, you can directly do that "in place".
Note: in other languages, like Kotlin, there is no such difference. There you only have Int objects, and call methods on those. But java makes that distinction. So study it, and learn how to use which approach.
Answer from GhostCat on Stack Overflow"Cannot invoke compareTo(int) on the primitive type int"
You have to understand: the primitive type int isn't a reference type like Integer. You can't call methods on primitive values. When you want to compare two ints, you can simply do index < newNumber for example.
Alternatively, you could use that static method compare of the Integer class, like: Integer.compare(index, newValue). But as said: you don't need methods to compare primitive values, you can directly do that "in place".
Note: in other languages, like Kotlin, there is no such difference. There you only have Int objects, and call methods on those. But java makes that distinction. So study it, and learn how to use which approach.
Edit: I can't believe what I originally wrote here.
Can you not just use a relational comparison operator here?
(firstNum < secondNum) // This will return a boolean value of true or false.
Everytime i try to use compareTo() in this. it just says "cannot invoke compareTo(int) on the primitive type int" can someone help me out here. what am i doing wrong?
``public int compareTo(Shape s) {
this.getAreaInt().compareTo(s.getAreaInt()); } protected abstract String getName(); protected abstract double getArea(); protected abstract int getAreaInt(); protected abstract double getPerimeter();
}
``
Well, the compiler's right :) You can't call compareTo directly. However, depending on the version of Java you're using, you can use Integer.compare (introduced in 1.7) and Double.compare (introduced in 1.4).
For example:
return Integer.compare(o1C.getPrice(), o2C.getPrice());
If you're not on 1.7 and still want to use built-in methods, you could use:
Integer price1 = o1C.getPrice();
Integer price2 = o2C.getPrice();
return price1.compareTo(price2);
... but this will use unnecessary boxing. Given that sorting a large collection can perform really quite a lot of comparisons, that's not ideal. It may be worth rewriting compare yourself, until you're ready to use 1.7. It's dead simple:
public static int compare(int x, int y) {
return x < y ? -1
: x > y ? 1
: 0;
}
Change the code
int price;
to
Integer price;
because primitive types such as int will not support any methods, like compareTo().
I do not understand why sometimes you can use .toString on an integer and other times it will say something along the lines of “cannot invoke .tostring on the primitive type int”. I was able to use toString all the time in class
Replace the call of an instance method compareTo with the call of static compare method, like this:
return Double.compare(array[index1], array[index2]);
This lets you keep your doubles in an array of primitives, and avoid autoboxing before calling an instance method.
In java primitive types don't have any methods. Instead using primitive data types use Wrapper classes.
change
return array[index1].compareTo(array[index2]);
to
return new Double(array[index1]).compareTo(array[index2]);
or
try with Double[] array; instead of double[] array;