Skip to content Skip to sidebar Skip to footer

Error: Incompatible Types: Boolean Cannot Be Converted To Int

I'm getting the error: incompatible types: boolean cannot be converted to int error while compiling the android app in Android Studio. The problem is in only one method. private st

Solution 1:

Your question is not bit clear but you might want this:

private static int get_wx_inx(Stringstr) {
        if(str.equalsIgnoreCase("木")) {
            return0;
        } elseif(str.equalsIgnoreCase("火")) {
            return1;
        }elseif(str.equalsIgnoreCase("土")) {
            return2;
        } elseif(str.equalsIgnoreCase("金")) {
            return3;
        } elseif(str.equalsIgnoreCase("水")) {
            return4;
        } else {
            return0;// write default return here. If every condition goes false then this default will be return.
        }
    }

Solution 2:

You can make use of Strings in switch statements and return respective int values.

https://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html

As we can't have mixed type for return value. You can consider 0(FALSE) and 1(TRUE). Or any such random value.

Solution 3:

Problem lies in this statement:

int equalsIgnoreCase2 = str.equalsIgnoreCase("火");

equalsIgnoreCase() method returns a boolean value and you are assigning it to an int value. That's why it is giving this error.

Solution 4:

Java is not a C language, so booleans cannot be integers, they are totally different types and cannot be mixed. In the 3rd line you are assigning boolean value to an int

Post a Comment for "Error: Incompatible Types: Boolean Cannot Be Converted To Int"