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 4 of 4

Thread: issue with Character.isUpperCase class

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default issue with Character.isUpperCase class

    Hi there, I'm having issues with my program. Only one part of the driver program is having issues. For that reason, I've only attached a small section of the code. I was wonder if anyone would be kind enough to explain why I keep having the errors:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range

    here is the code:

       import java.util.Scanner;
     
       public class characterTest
       {
          public static void main(String [] args)
          {
             Scanner input = new Scanner(System.in);
     
             String str;
     
             System.out.println("Enter string");
             str = input.next();
     
             int i, count3 = 0;
     
     
             for (i = 1; i <= str.length(); i++)
     
             {
                if (Character.isUpperCase(str.charAt(i)))
                   count3++;
             }
     
             System.out.println("your upper char: " +count3);
          }
       }


    Thank you for any advice!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,261
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: issue with Character.isUpperCase class

    Indexes begin at 0, so the last character in a String is at position length() - 1. In the code you posted
             for (i = 1; i <= str.length(); i++)
    the <= will eventually try to access the string at position length(), which is beyond its bounds.

  3. The Following User Says Thank You to copeg For This Useful Post:

    suxen (March 24th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: issue with Character.isUpperCase class

    Thanks Copeg for the hint. I'm very new to programming so it may take me a while to try to understand what you mean.

  5. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: issue with Character.isUpperCase class

    edit - I resolved it. I changed it to:

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

    Thanks a lot for your help!
    Last edited by suxen; March 24th, 2011 at 09:38 PM.

Similar Threads

  1. Help with Character arrays
    By shanklove in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 6th, 2010, 12:56 PM
  2. [SOLVED] last character position
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 05:31 AM
  3. Replies: 1
    Last Post: February 4th, 2010, 03:23 PM
  4. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 11:13 AM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM