I am trying the following code in Android Studio. When Debugging, I find out that even when Value of variable B is "(" my if statement does not execute and hovering over it, it shows it is false (please refer to image). The value of ScreenText in this case is "6(".
Any help is appreciated.
You should compare strings with the .equals()
method. In this case, and to prevent a null pointer exception in case that your B variable is null, you should do so like this:
if("(".equals(B)) {
...
}
The ==
operator is used for reference comparison, to check whether two object have the same reference.
You want to compare two String
s, use the boolean equals(String)
method.
if( "(".equals(B)) {
// your logic
}
Here comparing B
with "("
constant does not require you to make null
checks.
I think a more generic way to compare strings in Android is:
TextUtils.equals(B, "(");
And by the way, in your code
String.valueOf(c).toString();
toString() is redundant.
If you want to compare the value or contents of two strings then use .equals()
.
if(string.equals("(")){
//Enter your code
}
But if you want to check that two object points to same reference then use ==
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.