Search code examples
javaspringspring-bootspring-jdbc

Java: String index out of range: -4


i got an error "String index out of range: -4" heres my code below

if (WHEREClause.substring((WHEREClause.length() - 4)) == "AND ") {
    WHEREClause = WHEREClause.substring(0, (WHEREClause.length() - 4)); }

the value of WHERECLAUSE IS => "arSLC_CODE=13 AND "

and if i display "WHEREClause.substring((WHEREClause.length() - 4))" from the code i get this => "AND "

so it looks like if("AND " == "AND ")
{
...i go inside here
}

but instead i got an error "String index out of range: -4" and can't get inside the if statement. please help still new to this


Solution

  • When comparing strings use the .equals instead of ==. == is usually used only when comparing references or memory, while .equals is used when to see if they contain the same content