Nithis Kumar
Nithis Kumar

Reputation: 308

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 static int get_wx_inx(String str) {
    boolean equalsIgnoreCase = str.equalsIgnoreCase("木");
    int equalsIgnoreCase2 = str.equalsIgnoreCase("火");
    if (str.equalsIgnoreCase("土")) {
        equalsIgnoreCase2 = true;
    }
    if (str.equalsIgnoreCase("金")) {
        equalsIgnoreCase2 = 3;
    }
    return str.equalsIgnoreCase("水") ? 4 : equalsIgnoreCase2;
}

I don't know what could be wrong. Can you help me to figure out the problem. Thanks in advance.

Upvotes: 1

Views: 15676

Answers (4)

Rumit Patel
Rumit Patel

Reputation: 12619

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

private static int get_wx_inx(String str) {
        if(str.equalsIgnoreCase("木")) {
            return 0;
        } else if(str.equalsIgnoreCase("火")) {
            return 1;
        }else if(str.equalsIgnoreCase("土")) {
            return 2;
        } else if(str.equalsIgnoreCase("金")) {
            return 3;
        } else if(str.equalsIgnoreCase("水")) {
            return 4;
        } else {
            return 0;// write default return here. If every condition goes false then this default will be return.
        }
    }

Upvotes: 2

Dinesh Acharya
Dinesh Acharya

Reputation: 135

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.

Upvotes: 2

maslan
maslan

Reputation: 2188

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

Upvotes: 1

Mushif Ali Nawaz
Mushif Ali Nawaz

Reputation: 3866

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.

Upvotes: 1

Related Questions