My Android app crashes during if
statement and I can not find the mistake.
Here is a snippet of my code, how could I fix it?
public String formatResult(String inVal) {
String tmp = inVal.substring(inVal.length() - 2, inVal.length());
if (tmp.equals(".0") == true){
return inVal.substring(inVal.length(),inVal.length()-2);
} else {
return inVal;
}
}
inVal.substring(inVal.length(),inVal.length()-2);
not very valid since:
public String substring(int beginIndex,
int endIndex)
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
"inVal" can be shorter than 2 characters. Debug the project and watch inVal, in any case put if-statements to control if inVal is longer than 2.
You can change:
if (tmp.equals(".0") == true)
to:
if (tmp.equals(".0")){
Another thing is, do you really want to return:
inVal.substring(inVal.length(),inVal.length()-2);
or you want to return:
inVal.substring(inVal.length()-2, inVal.length());
EndIndex cannot be smaller than StartIndex. If you want to start from end to end-2:
string a;
for(int i = inVal.length; i>=inVal.length-2; i--) {
a += inVal.charAt(i);
}
This should work.
String a = editboxa.getText().toString();
String b = editboxb.getText().toString();
if(a.equals(b))
//do something
else
//do something
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.