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: String out of bound exception

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

    Default String out of bound exception

    what does "java.lang.stringindexoutofboundsexception string index out of range: -2" mean? I know the -1 thing but what does -2 imply?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: String out of bound exception

    The -2 is the value of the index that was out of bounds.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String out of bound exception

    Hello,

    I would like to provide an example that will clarify this exception but before that we need to look at the substring(int beginIndex, int endIndex) method. The two method parameters int beginIndex and int endIndex have a key difference:

    beginIndex is 0 based
    endIndex is 1 based

    And here is an example to illustrate this:

    public static void main(String[] args) {
         String str = "012345";
         System.out.println(str.substring(0, 1);
    }

    This will print "0" and here is why

    0 based indexes of our string:
    String str = "012345";
    INDEXES 012345

    1 based indexes of our string:
    String str = "012345";
    INDEXES 123456

    you can play with the indexes and you can even say str.substring(3, 3) which will print an empty string "".
    Now to go back to our exception java.lang.StringIndexOutOfBoundsException -2
    This exception will be thrown when the endIndex is beginIndex = endIndex - 2 which in our case is str.substring(3, 1)

    The following code will throw java.lang.StringIndexOutOfBoundsException -2:

    public static void main(String[] args) {
         String str = "012345";
         System.out.println(str.substring(3, 1);
    }

    I hope this helps.

    Regards
    Alin
    Last edited by Alin; August 2nd, 2018 at 05:43 AM.

Similar Threads

  1. generating pdf throgh itextrenderer throwing index out of bound exception
    By dreamsshinu in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 11th, 2018, 12:31 PM
  2. Invalid String Parameter Exception
    By ch103 in forum Exceptions
    Replies: 21
    Last Post: November 6th, 2011, 08:00 PM
  3. [SOLVED] Problem using the length of a string to bound the number of iterations of a for loop
    By dtitt3 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 3rd, 2011, 01:44 PM
  4. [SOLVED] Nullpointer exception when sending string
    By treshr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 26th, 2011, 04:36 AM
  5. Need help with exception handling for a string
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 6th, 2011, 06:59 AM