Java list index out of range

In java, what is the best way to switch characters in a string while keeping the rest of the characters in their original spot. My real concern is if there's a way to deal with this index out of range error other than the following:
Let's say you want to switch the letter r with the capital E in "stringExample".

If you're looking to just replace characters in a string, you're probably best off rebuilding your string entirely using String.substring. For your example, you'd simply need to use temporary variables to hold the letters you want to swap. For example: