Like jps said, you're taking the number of the last element in your string, and adding 1 to it. You can solve this by simply saying either:

for(int i = 0; i <= str.length() - 1; i++)

or the...