Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incomparable type in java

Tags:

java

I have got this code, and I get an error incomparable types: java.lang.String and int, for this line of code if ((this.name.String.compareTo(obj.name == 0)) && (this.age = obj.age))

The method is this:

public int compareTo(Object o)
{
    int result;
    AnyClass obj = (AnyClass)o;
    if ((this.name.String.compareTo(obj.name == 0)) && (this.age = obj.age))
    {
        result = 0;
    }
   else if (this.name.compareTo(obj.name) > 0)
    {
        result = 1;
    }
    else
    {
        result = -1;
    }
    return result;
}
like image 418
user1928374 Avatar asked Jan 27 '26 04:01

user1928374


1 Answers

I think that position of your bracket isn't correct,

this.name.String.compareTo(obj.name == 0)) 

obj.name == 0 is the place where you probably compare String (name) to int (0). I guess you wanted to use compareTo on obj.name and then check if it's equal to zero.

I also think that in the second part

(this.age = obj.age)

You wanted to use == instead of =, so I think that the code you wanted to use is:

((this.name.compareTo(obj.name)==0) && (this.age == obj.age))
like image 187
Paweł Chorążyk Avatar answered Jan 29 '26 18:01

Paweł Chorążyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!