why is it showing stringoutofbounds exception while i am only comparing the 0th index of two strings?

77 Views Asked by At

I was doing a leetcode problem and I got a String index out of range: 0 exception for the below code:

    public boolean isInterleave(String s1, String s2, String s3) {
        int n = 0;
        int m = 0;
            
        if (s1.charAt(0) == s3.charAt(0)) {
            n++;

            for (int j = 1; j < s1.length(); j++) {
                if (s1.charAt(j) == s3.charAt(j)) {
                    n++;                        
                } else {
                    break;
                }
            }

            for (int j = 0; j < s2.length(); j++) {
                if (s2.charAt(j) == s3.charAt(j + n)) {
                    m++;                        
                } else {
                    break;
                }
            }
        }

        if (n > 0) {
            System.out.println(n);
        } else {
            System.out.println(m);
        }
    }

I tried to count how many characters of two strings are the same for a string like in the problem of interleaving String question and expected to print 2,2 for the input s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"

2

There are 2 best solutions below

0
Andrés Alcarraz On

Te why, is because an empty string does not have a char at position 0, but you just don't need that first if.

The first for loop will not enter if the string s1 is empty but the second will.

You could add a if before that for checking for emptyness, but I don't know what you should return in that case, something like this:

public boolean isInterleave(String s1, String s2, String s3) {
    if (s1.isEmpty() || s2.isEmpty() || s3.isEmpty() {
        return ...; //what's appropriate for this case
    }
... //your code.
}

I'm not saying this is what you should do to resolve your problem, just how to avoid the error you are having and as a form of exemplifying why you are having it.

0
Reilas On

"why is it showing stringoutofbounds exception while i am only comparing the 0th index of two strings? ..."

If I run this code with the Interleaving String problem on LeetCode, I get the following error.

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
  at line 48, java.base/java.lang.StringLatin1.charAt
  at line 1513, java.base/java.lang.String.charAt
  at line 6, Solution.isInterleave
  at line 54, __DriverSolution__.__helper__
  at line 90, __Driver__.main

Line 6 is,

if (s1.charAt(0) == s3.charAt(0))

In situations where s1 or s3 are empty, the String#charAt method will throw an IndexOutOfBoundsException, which is the parent class of StringIndexOutOfBoundsException.

Here is the String#charAt JavaDoc,

Throws:

IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.

You'll have to check both s1 and s3 for content.
I'm using the String#isEmpty method here, which checks String#length for 0.

if ((!s1.isEmpty() && !s3.isEmpty()) && s1.charAt(0) == s3.charAt(0))

This should get you to the next step in the process.