user3212766
user3212766

Reputation: 5

I am having trouble getting substring to work

This method is supposed to get the number of occurrences of a certain pattern and return the int value. I keep getting this error

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

Code

public int getCount(String pattern){
    int occerenceOfPattern = 0;
    for (int i = 0; i <= strand.length(); i++) {
        if (strand.substring(i, i + pattern.length()) == pattern) {
            occerenceOfPattern++;
        }
    }
    return occerenceOfPattern;
}   

Upvotes: 0

Views: 90

Answers (7)

spgodara
spgodara

Reputation: 1084

You need to correct your condition check in loop and also add new check inside loop block:

public int getCount(String pattern){
    int occerenceOfPattern = 0;
    for (int i = 0; i < strand.length(); i++) { // Updated check
        if((i + pattern.length()) >= strand.length()) // New condition to avoid exception
            break;
        if (strand.substring(i, i + pattern.length()) == pattern) {
            occerenceOfPattern++;
        }
    }
    return occerenceOfPattern;
}

New added check can also be handled in loop condition itself.

Upvotes: 0

pratim_b
pratim_b

Reputation: 1200

public int getCount(String pattern){
    int occerenceOfPattern = 0;
    for (int i = 0; i < strand.length(); i++) {
        if (strand.substring(i, i + pattern.length()) .equals(pattern)) {
            occerenceOfPattern++;
        }
    }
    return occerenceOfPattern;
} 

(changed == to .equals. for reason see this post) Use equalIgnoreCase if it is case insensitive.

length() is already described in rest of the answers

== tests for reference equality.

.equals() tests for value equality.

How to compare Strings in java

Upvotes: 1

Ted Bigham
Ted Bigham

Reputation: 4341

3 problems...

Change <= to < in your loop.

You also need to limit the right side of the substring to not be past the end of the string.

And you need to use .equals() not ==.

public int getCount(String pattern){
    int occerenceOfPattern = 0;
    for (int i = 0; i < strand.length(); i++) {
        if (strand.substring(i, Math.min(i + pattern.length(), strand.length())).equals(pattern)) {
            occerenceOfPattern++;
        }
    }
    return occerenceOfPattern;
}  

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

i <= strand.length() in your for loop is your problem...

length() returns the number of elements in an Array. Always remember that index starts from 0. So, if length is 5, you have 5 elements 0,1,2,3 and 4. So, you have to use i<strand.length();

You get StringIndexOutOfBoundsException because element with index "length-1" is the last element and you are trying to access element with index="length".

Upvotes: 0

user2535749
user2535749

Reputation: 35

    i <= strand.length()

.length() returns the total length of the string and the indexes of the string start at 0. So if i is equal to the string length you will get an out of bounds. To fix this use:

    i <= strand.length() - 1

or

    i < strand.length()

Upvotes: 2

Jainendra
Jainendra

Reputation: 25143

StringIndexOutOfBoundsException comes when the index where you are pointing to is null(does not exist). Here the problem I see is in strand.length().

for (int i = 0; i < strand.length(); i++)

This should work fine

Upvotes: 1

Makoto
Makoto

Reputation: 106430

You're iterating too far on your String.

For substring, charAt, or any method that requires to you to use an exact numerical value to get at a character or a group of characters, the size of the String is defined as the result of the length() call minus 1.

It's like an array (since it is backed by a char[]): "cat" has length 3, but it's zero based, so I can only go up to 2.

Change your condition to be strictly less-than, and not less-than or equal to.

Upvotes: 1

Related Questions