There is a substring() method call on line 39 where the value of the index is out of bounds/past the end of the String.
Look at the code on that line and see why: how long is the String & what is the value of the index.
Use a println to print out those values if you need to see what they are.
One problem with the code
is the last else does NOT test the first letter of the function String. It should test for that and there should be an ending else clause that prints an error message if none of the above test were true.