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

Thread: error string index out of range

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default error string index out of range

    hello everybody
    i write a code but it show a error message
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
    ex out of range: 10
    at java.lang.String.charAt(String.java:658)
    at StringChar.main(StringChar.java:11)

    plz help me and this is my first post in this forum
    class StringChar{
     
    	public static void main(String ss[]){
     
    		String str="HelloWorld";
    		int a;
    		System.out.println("String is = " + str);
    		a=str.length();
    		System.out.println("String is After Reverse");
    		for(int i=a;i>=0;i--)
    			System.out.print(str.charAt(i));
    	}
    }
    Last edited by moaz amin; July 2nd, 2014 at 03:16 PM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: error string index out of range

    you are trying to access element at index 10. But actually, the element with the highest index is 9.
    The length of the array is 10 (base count is 1). The highest index is 9 (base count is 0).

    Therefore. an array of length 10 has indexes 0-9 only.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: error string index out of range

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: error string index out of range

    i understand my fault but plz help me and tell me how i solve this problem

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: error string index out of range

    The max index is the array length-1. The code needs to set its starting index to the max value(length-1), not the length of the array.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: February 27th, 2013, 10:36 AM
  2. String Index Out of range
    By Shikha Jain in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 10:14 PM
  3. String index is out of range
    By etag in forum Exceptions
    Replies: 9
    Last Post: April 4th, 2012, 10:59 PM
  4. String index out of range?
    By oksmartypants in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 02:10 PM
  5. String Index out of range
    By petemyster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 03:59 PM