Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why do I get an exception in this simple loop?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Why do I get an exception in this simple loop?

    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:
    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)
    Thanks.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why do I get an exception in this simple loop?

    for (int i = 0; i <= str.length(); i++)


    str.length is = the index number of the last element + 1

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why do I get an exception in this simple loop?

    Like jps said, you're taking the number of the last element in your string, and adding 1 to it. You can solve this by simply saying either:

    for(int i = 0; i <= str.length() - 1; i++)

    or the one I think is better

    for(int i = 0; i < str.length(); i++)

Similar Threads

  1. Simple Do Loop Problem
    By inflames098 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 03:43 PM
  2. [SOLVED] WHILE LOOP USING || (or) SIMPLE PROBLEM
    By SPACE MONKEY in forum Loops & Control Statements
    Replies: 3
    Last Post: May 7th, 2012, 12:23 AM
  3. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  4. [SOLVED] ArrayOutofBoundary in Simple While Loop
    By Johnpower in forum Loops & Control Statements
    Replies: 6
    Last Post: June 13th, 2010, 04:31 AM
  5. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM