In java, you cannot use .compareTo() to compare primitive types (like an int) with something else.
Is there a way to compare if two integers are less than zero? I am trying to compare an integer to a spot in an array.
Thanks!
SOLVED: Sorry everyone, had a massive brain cramp for a second. I forgot that a.compareTo(b) returns -1, 0, or 1 based on if the first value is less than(returns -1), equal to(returns 0),or greater than (returns 1). My code said int.compareTo(array)<0 when really all it needed to say was int
use like this
if(firstinteger<0 && secondinteger<0){
}
==
if compared against primitives will either perform numerical or boolean equality . That said, for what you want, it should be as trivial as doing this:
if(firstValue < 0 && secondValue < 0) {
// operate
}
Remember: You only use .compareTo
for references that implement Comparable
(such as Integer
, Long
, etc). You use ==
for all other numerical primitives. 1
1: But watch out for floating-point values. Those may not always equal each other.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.