I make an if condition to check if the specified string contains "me" at the end.
Given Return
----- ------
Lame True
Meant False
Come True
etc
At the moment, my code works fine if the string length is more than 2 characters.
public boolean containsLy(String input) {
String ly = "ly";
String lastString = input.substring(input.length() - 2);
if (input.length() < 2) {
return false;
}else if (lastString.equals(ly)) {
return true;
}else
return false;
}
But whenever a line has 2 characters or less, I get this error:
StringIndexOutOfBoundsException
This is obviously due to a negative number, but I can't come up with a workaround for this.
source
share