Results 1 to 5 of 5
  1. #1
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default String Index Out of Bounds Exception

    Here is a sample code of something I am working on. This sample code reproduces the error. I am trying to return whatever is after the specified index but it keeps saying "out of bounds". Any help would be appreciated.


    Java Code:
    public class test 
    {
    	static String divLine2 = " IS ";
    	static String uInput = "A DOG IS TEST";
    	
    	public static void main(String[] args)
    	{
    		int i = uInput.indexOf(divLine2 + divLine2.length());
    		System.out.println(uInput.substring(i));
    	}
    	
    }
    The stack error reads :
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1931)
    at java.lang.String.substring(String.java:1904)
    at test.main(test.java:13)

    The line that is giving the error is:

    System.out.println(uInput.substring(i));

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: String Index Out of Bounds Exception

    Print out the value of 'divLine2 + divLine2.length()' and see what's happening.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: String Index Out of Bounds Exception

    It gives me -1. That is interesting. Why is that happening?

  4. #4
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: String Index Out of Bounds Exception

    Found the solution. Needed to do the math OUTSIDE the function like so:

    Java Code:
    public class test 
    {
    	static String divLine2 = " IS ";
    	static String uInput = "A DOG IS TEST";
    	
    	public static void main(String[] args)
    	{
    		int i = uInput.indexOf(divLine2);
    		int j = divLine2.length();
    		j = i+j;
    		System.out.println(j);
    		System.out.println(uInput.substring(j));
    	}
    	
    }
    Thanks for your help.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: String Index Out of Bounds Exception

    You're welcome of course; that simple System.out.println( ... ) statement can be a handy, cheap debugging tool.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. array index out of bounds exception, AGAIN!
    By samanyu in forum New To Java
    Replies: 8
    Last Post: 06-08-2011, 12:27 PM
  2. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 11:24 PM
  3. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-19-2010, 12:25 AM
  4. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 05:02 PM
  5. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •