java.lang.StringIndexOutOfBoundsException:字符串索引超出范围

时间:2012-02-09 23:01:02

标签: java eclipse string substring

您好我写了一个java代码来查找由其他单词构成的最长单词。我的逻辑是从文本文件中读取单词列表并将每个单词添加到一个数组中(在文本中单词是排序的,每行中只有一个单词)之后我们检查数组中的每个元素是否都有其他元素作为子串。如果是这样,我们计算子串的数量。具有最大子串数的元素将是结果

当我给出一个只有两个单词的文本文件时代码正在运行。但是,如果有两个以上的单词,我会听到错误

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:3

如果(s.charAt(i1)== w.charAt(j1))

,我觉得此行中出现错误
  import java.util.*;
  import java.io.*;
  import java.lang.reflect.Array;
  public class Parser
  {
public static void main (String[] args) throws IOException
{
    String [] addyArray = null;

    FileReader inFile = new FileReader ("sample.txt");
    BufferedReader in = new BufferedReader (inFile);
    String line = "";
    int a = 0;
    int size=0;
    String smallestelement = "";

    while(in.ready())
    {
        line=in.readLine();
        while (line != null && line != "\n")
        {
            size++;
            System.out.println(size);
            line = in.readLine();
            if (line == null) line = "\n";
        }
    }
    addyArray = new String[size];
    FileReader inFile2 = new FileReader ("sample.txt");
    BufferedReader in2 = new BufferedReader (inFile2);
    String line2 = "";

    while(in2.ready())
    {
        line2 = in2.readLine();


        while (line2 != null && line2 != "\n")
        {

            addyArray[a] = line2;


            System.out.println("Array"+addyArray[a]);
            line2 = in.readLine();
            a++;
            if (line2 == null) line2 = "\n";
        }

    }


    int numberofsubstrings=0;
    int[] substringarray= new int[size];

    int count=0,no=0;

for(int i=0;i<size;i++)
{       
    System.out.println("sentence "+addyArray[i]);
    for(int j=0;j<size;j++)
    {
        System.out.println("word "+addyArray[j]);

        String w,s;
        s=addyArray[i].trim();
        w=addyArray[j].trim();

        try{
            for(int i1=0;i1<s.length();i1++)
            {
                if(s.equals(w)&& s.indexOf(addyArray[j-1].trim()) == -1)
                {}
            else
            {
                if(s.charAt(i1)==w.charAt(0))
                {                   
                   for(int j1=0;j1<w.length();j1++,i1++)
                   {
                     if(s.charAt(i1)==w.charAt(j1)) //I feel the error is occuring here
                     { count=count+1;}
                       if(count==w.length())
                       {no=no+1;count=0;};  

                   }
                }
              }
            }
              System.out.println(no);
        }
        catch(Exception e){System.out.println(e);}
        substringarray[i]=no;
        no=0;

        }
    }   



        for(int i=0;i<size;i++)
        {
            System.out.println("Substring array"+substringarray[i]);
        }
    Arrays.sort(substringarray);  
    int max=substringarray[0];

    System.out.println("Final result is"+addyArray[max]+size);

}
    }

3 个答案:

答案 0 :(得分:6)

这是问题所在:

 for(int j1=0;j1<w.length();j1++,i1++)

在循环的每次迭代中,您都会递增i1以及j1i1可能已经在s的末尾,所以在您递增之后,s.charAt(i1)将无效。

两个旁白:

  • 您应该查看String.regionMatches
  • 使用一致的缩进和合理的空格可以使您的代码更多更容易阅读。

答案 1 :(得分:1)

使用string.charAt(x)时,必须检查它是否超出字符串长度。 Documentation表示如果index参数为负或不小于此字符串的长度,您将获得“IndexOutOfBoundsException”。在你的特定情况下,你只是在循环中验证你在w长度下,所以它会失败。

正如SO已经说过的那样,循环只考虑w长度,所以如果你有一个较短的s,它将引发该异常。检查条件,使其上升到较短的字符串或重新考虑该过程。

答案 2 :(得分:1)

一些提示:

首先,当您要求调试帮助时,始终包含完整堆栈跟踪。它应该指出问题正在发生的确切行号。

其次,除了for(int j1=0;j1<w.length();j1++,i1++)之外,你的问题很可能在你的内部循环i1增加j1,这将导致i1最终超出字符串s

最后,您应该考虑对字符串使用String.contains()方法,甚至使用正则表达式。

相关问题