java.lang.StringIndexOutOfBoundsException : String index out of range 异常

标签 java eclipse string substring

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

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

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

我觉得错误发生在这一行 if(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);

}
    }

最佳答案

问题是:

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

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

两个旁白:

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

关于java.lang.StringIndexOutOfBoundsException : String index out of range 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9220281/

相关文章:

java - ContentProposalAdapter 接受通过键盘选择的建议,但不接受通过鼠标选择的建议

c - 数组元素作为不同数组的索引

java - OpenCV2.4.8 Android SDK 为 "The import java.util cannot be resolved"

php - 从字符串创建二维数组,但忽略特定字符

java - java Controller 中无法正确识别特殊/重音字符?

java - 让 JQuery 作用于 GWT 动态添加的类名

Java 进程内存使用情况(jcmd 与 pmap)

java - 没有 SSL 的 Active Directory 密码重置

java - hadoop 将 int 数组从 map 传递到 reducer 并作为输出

python - python中使用self.browse进行openerp开发的目的是什么?