#Index error
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Error:- Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
My code.
write out your math, u will notice
Is i+1 included in the substring function?
index 3 out of bounds for length 3 is your error message
Since your loop starts from 0, i should be {0,1,2} for a string that has 3 characters
so when we get to the last loop i.e. i is 2
You're doing i+1 which becomes 3 and your substring fails with an exception
A simple way to do palindrome would be to have 2 variables:
originalString reversedString
And then loop over each character in originalString and match it with reversedString once you get 2 matches, stop looping and return true