Hey there guys. I'm new to loops and I'm trying to write a simple code that outputs the number of uppercase letters in a string. However, I get an exception when I run the code. What's wrong? Here is the code:
public class CountingMatches { public static void main(String[] args) { String str = "Hello, World!"; int upperCaseLetters = 0; for (int i = 0; i <= str.length(); i++) { char ch = str.charAt(i); if(Character.isUpperCase(ch)); { upperCaseLetters++; } } System.out.println(upperCaseLetters); } }
And this is the exception I get:
Thanks.Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 13
at java.lang.String.charAt(Unknown Source)
at CountingMatches.main(CountingMatches.java:11)