简体   繁体   English

无法在基本类型boolean上调用equals(boolean)

[英]Cannot invoke equals(boolean) on the primitive type boolean

I want to check equality of first and last two characters of a string so i have written condition like 我想检查字符串的前两个字符是否相等,所以我写的条件如下

if (str.length() >= 4
            && ((str.startsWith(str.substring(0, 2))).equals(str.endsWith(str
                    .substring(str.length() - 3, str.length() - 1)))))

but I am getting error as Cannot invoke equals(boolean) on the primitive type boolean 但由于无法在原始类型boolean上调用equals(boolean)而出现错误

so what is the root cause? 那么根本原因是什么?

Error is coming because : str.startsWith() returns boolean value and we are calling equals() with Boolean 由于即将出现错误,因为:str.startsWith()返回boolean值,并且我们使用Boolean调用equals()

Use this expression to compare : 使用此表达式进行比较:

    str.substring(0, 2).equals(str.substring(str.length() - 3, str.length() - 1))

I want to check equality of first and last two characters of a string 我想检查字符串的前两个字符是否相等

That's not what you're doing though. 那不是你在做什么。 You're using startsWith and endsWith - you're asking whether a string starts with its own first two characters, and whether it ends with some portion of the string near the end, and then trying to compare the results of those comparisons... except that you're trying to compare two boolean values with equals instead of == , which isn't going to work either. 您正在使用startsWithendsWith –您要询问字符串是否以其自己的前两个字符开头,以及它是否以字符串的结尾附近部分结尾,然后尝试比较这些比较的结果...除了您尝试将两个boolean值与equals而不是==进行比较之外,这也不起作用。

You just want substring and equals here - but your substring is incorrect too, in that you have an off-by-one error for finding the last two characters. 您只想在此使用substringequals -但您的substring也不正确,因为在查找最后两个字符时会有一个错误的错误。 I would personally split this up for simplicity: 为了简单起见,我个人将其拆分:

if (str.length() > 4) {
    String start = str.substring(0, 2);
    // If you don't specify an end point, it'll be "the rest of the string"
    // which is what you want.
    String end = str.substring(str.length() - 2);
    if (start.equals(end)) {
        ...
    }
}

endsWith api will return you a boolean value and hence compiler is compiling that you are trying to compare String with boolean. startswith api将返回一个布尔值,因此编译器正在编译您试图将String与boolean进行比较。 Instead you could do something like: 相反,您可以执行以下操作:

if (str.endsWith(str.substring(0, 2))) {
     //ye it does ends with same string as it starts with
}

The method startsWith(String) returns a boolean indicating if the string it is being applied on effectively starts with the string argument. startsWith(String)方法返回一个布尔值,该布尔值指示是否有效地对其应用的字符串以string参数开头。 For comparing the first two characters with the last two ones, your boolean condition can be: 为了比较前两个字符和后两个字符,您的布尔条件可以是:

if(str.length() >= 4 && str.startsWith(str.substring(str.length - 3, str.length()))

Be careful with the indices in the substring method since the last parameter indicates the place of the first character not to be included in the substring. 请注意子字符串方法中的索引,因为最后一个参数指示不包括在子字符串中的第一个字符的位置。 Same result but with the endsWith(String) method: 结果相同,但使用endsWith(String)方法:

if(str.length() >= 4 && str.endsWith(str.substring(0, 3)))

Or with only sunbstring(int, int) method: 或仅使用sunbstring(int, int)方法:

if(str.length() >= 4 
&& str.substring(0, 3).equals(str.substring(str.length() - 3, str.length())))

Your problem stems from 您的问题源于

((str.startsWith(str.substring(0, 2))).equals(str.endsWith(str
                .substring(str.length() - 3, str.length() - 1))))

Which is basically coming down to boolean.equals(...) , which can't be done, as boolean is not an Object 这基本上归结为boolean.equals(...) ,因为boolean不是Object ,所以无法完成

You could, instead, make use of String#startsWith and String#endsWith , for example... 例如,您可以使用String#startsWithString#endsWith ...

if (str.length() >= 4 &&
    str.endsWith(str.substring(0, 2)) &&
    str.startsWith(str.substring(str.length() - 2))) {

Or maybe even 甚至

if (str.length() >= 4 &&
    str.substring(0, 2).equals(str.substring(str.length() - 2))) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM