1

I am getting the following error when calling a function from within my class: java.lang.StringIndexOutOfBoundsException: String index out of range: -1. Although I used a system prints to see the inputs I am passing in the substring() function and everything seems to be right. The function isContained() returns a boolean value defining whether the substring passed as a parameter is in a list of words. My code is:

for(int i=0; i<=size; i++)
    for(int j=i+1; j<=size; j++)
        if(isContained(str.substring(i,j-i)))
            System.out.println(str.substring(i,j-i));

where size is the size of the string (str) I am passing in the function

5
  • i think the problem is when i=0 and j = size. Commented Nov 23, 2013 at 12:16
  • 2
    did u even tried to debug it Commented Nov 23, 2013 at 12:17
  • I don't think the problem is there fly. The error comes up for i=1.. Commented Nov 23, 2013 at 12:21
  • Try to Debug your program. Put printouts(i could read that you already tried that), so use a Debugger. If you are using Eclipse IDE it has a built in Debugger. Commented Nov 23, 2013 at 12:21
  • 1
    I am afraid I already did Commented Nov 23, 2013 at 12:23

3 Answers 3

7

You are calling str.substring(i, j-i) which means substring(beginIndex, endIndex), not substring(beginIndex, lengthOfNewString).

In documentation of this method we can read (emphasis mine):

Throws:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

So endIndex must be greater or equal to beginIndex. If that assumption is not fulfilled method throws StringIndexOutOfBoundsException (which is subclass of IndexOutOfBoundsException).

If I understand your example correctly you should change it to str.substring(i, j).


Also if size is length of your str then

for (int i = 0; i <= size; i++)

should most likely be

for (int i = 0; i < size; i++)
Sign up to request clarification or add additional context in comments.

1 Comment

You are right... I thought the method was accepting (index, length)
0

I think you need to change the looping condition which is the problem here. You are looping one more iteration when you do <=size and the index starts from i=0. You can change this

for(int i=0; i<=size; i++)

to

for(int i=0; i<size; i++)

and also take care about the inner loop condition.

1 Comment

Why negative voting here?
0

IndexOutOfBoundsException -- if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

Actually, your right edge of your substring function may be lower than the left one. For example, when i=(size-1) and j=size, you are going to compute substring(size-1, 1). This is the cause of you error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.