You are checking the capital letters twice in your loop, you should increment i by 1 when you add in a space. This will also mean you don't need to use i + 1 to de-capitalise the current letter
str.insert(i, ' ');
i++;
char ch = Character.toLowerCase(str.charAt(i));
str.setCharAt(i, ch);