java.lang.StringIndexOutOfBoundsException: String index out of range

后端 未结 3 1842
别跟我提以往
别跟我提以往 2021-01-06 16:56

Hi I wrote a java code to find longest word made of other words. My logic is to read the list of words from the text file and add each word into an array (In the text the wo

3条回答
  •  天涯浪人
    2021-01-06 17:30

    This is the problem:

     for(int j1=0;j1

    On each iteration through the loop, you're incrementing i1 as well as j1. i1 could already be at the end of s, so after you've incremented it, s.charAt(i1) is going to be invalid.

    Two asides:

    • You should look at String.regionMatches
    • Using consistent indentation and sensible whitespace can make your code much easier to read.

提交回复
热议问题