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
so what is the root cause?
Error is coming because : str.startsWith() returns boolean
value and we are calling equals()
with Boolean
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.
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. 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. 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. 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:
if(str.length() >= 4 && str.endsWith(str.substring(0, 3)))
Or with only sunbstring(int, int)
method:
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
You could, instead, make use of String#startsWith
and String#endsWith
, for example...
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))) {
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.