Stackoverflow when checking for one row in another

I currently have a method that should take two lines and then check if one line exists as a substring in another. It does not check both paths, so the method of passing strings to a method determines which string to look for in another.

I am currently getting stackoverflow error .

public boolean checkMatch(String text, String regex, int i){

    int regexL = regex.length();

        if(i == regexL){
            return true;
        }   

        System.out.println(text.charAt(i) + " " + regex.charAt(i));

        if(text.charAt(i) == regex.charAt(i)){
            return checkMatch(text, regex, i++);
        }
        else if(text.charAt(i) != regex.charAt(i)){

            if(text.substring(1) == ""){
                return false;
            }               
            else if(text.substring(1) != ""){
                return checkMatch(text.substring(1), regex, 0);
            }
        }
        return false;

}

I run a test using my first name as an example.

@Test public void test_52() {
    assertEquals(true, checkMatch("Samual", "mu", 0));
}

the console looks as soon as it overflows.

S m

am

mm

mm

mm

etc.

Where am I mistaken? Am I really wrong? The stack trace shows that it seems to fall on this line.

return checkMatch(text, regex, i++);

But a defect point is rarely a point of failure. Sorry for the wall of text and code.

+4
3

, , : i++ i . . , ++i.

+9

:

return checkMatch(text, regex, i++);

:

return checkMatch(text, regex, ++i);

:

return checkMatch(text, regex, i + 1);

i++ i, i, .

i , .

+3

indexOf:

 System.out.println("Samual".indexOf("mu") > 0);

:

true
0

Source: https://habr.com/ru/post/1531107/


All Articles