So I\'m trying to find all the uppercase letters in a string put in by the user but I keep getting this runtime error:
Exception in thread \"main\" java.lan
The array index out of bounds is due to the for loop not terminating on length - 1
, it is terminating on length
Most iterating for loops should be in the form:
for (int i = 0; i < array.length; i++) {
// access array[i];
}
It's the same with a string.
Perhaps a cleaner way would be:
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
Doesn't implement Iterable
, silly Java.