below is my code for Leetcode 9 Palindrome Number. It shows "String index out of range error" for line 8 when the testcase is 121. Can anyone tell me why this happened? Thanks!
class Solution {
public boolean isPalindrome(int x) {
String s = String.valueOf(x);
int i = 0;
int j = s.length()-1;
while(i < j){
if(s.charAt(i) != s.charAt(j)){ <------ line 8
return false;
}else{
s = s.substring(i+1,j);
}
}return true;
}
}