The index of the array beyond the bounds is due to the fact that the for loop does not end with length - 1 , it ends with length Most iterations for loops should be in the form:
for (int i = 0; i < array.length; i++) {
Same thing with the string.
Perhaps a cleaner way:
String inputString; // get user input String outputString = ""; for (int i = 0; i < inputString.length; i++) { c = inputString.charAt(i); outputString += Character.isUpperCase(c) ? c + " " : ""; } System.out.println(outputString);
Edit: Forgot String Does not implement Iterable<Character> , stupid Java.
source share