Finding all uppercase letters of a string in java

java, lang, printing, string, uppercase

Solution

for(int y = 0; y <= z; y++){

should be

for(int y = 0; y < z; y++){

Remember array index starts from ZERO.

String length returns

the number of 16-bit Unicode characters in the string

Because loop started from ZERO, loop should terminate at length-1.

Problem

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.lang.StringIndexOutOfBoundsException: String index out of range: 4 at java.lang.String.charAt(String.java:686) at P43.main(P43.java:13) ``` I feel foolish but I just can't figure this out and oracle even talks about charAt on the page about java.lang.StringIndexOutOfBoundsException Here is my code for finding the uppercase letters and printing them: ``` import java.io.*; import java.util.*; public class P43{ public static void main(String[] args){ Scanner in = new Scanner(System.in); //Uppercase String isUp = ""; System.out.print("Please give a string: "); String x = in.next(); int z = x.length(); for(int y = 0; y <= z; y++){ if(Character.isUpperCase(x.charAt(y))){ char w = x.charAt(y); isUp = isUp + w + " "; } } System.out.println("The uppercase characters are " + isUp); //Uppercase } } ``` I'd really appreciate any input and or help.

Original source

Related problems