Replace:
j <= input.length()
... with ...
j < input.length()
Java String
character indexing is 0-based, so your loop termination condition should be at input
's length - 1.
Currently, when your loop reaches the penultimate iteration before termination, it references input
character at an index equal to input
's length, which throws the StringIndexOutOfBoundsException
(a RuntimeException
).