java.lang.StringIndexOutOfBoundsException:String索引超出范围 [英] java.lang.StringIndexOutOfBoundsException: String index out of range

查看:98
本文介绍了java.lang.StringIndexOutOfBoundsException:String索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



java.lang.StringIndexOutOfBoundsException:String索引超出范围: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 minimizeelement =;

while(in.ready())
{
line = in.readLine();
while(line!= null&& line!=\\\

{
size ++;
System.out.println(size);
line = in.readLine();
if(line == null)line =\\\
;
}
}
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!=\\\

{

addyArray [a] = line2 ;


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

}


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

int count = 0,no = 0; (int i = 0; i< size; i ++)
{
System.out.println(sentence+ addyArray [i]);

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

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

尝试{
for(int i1 = 0; 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 {
if(s。 charAt(i1)== w.charAt(j1))//我觉得错误发生在这里
{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   

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



两个旁边:




  • 您应该查看 String.regionMatches

  • 使用一致的缩进和明智的空格可以使您的代码更容易阅读。


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 words are sorted and there will be only one word in each line) After that we check if each element in the array has other elemnts as substrings. If so we count the number of substrings. The element with maximum number of substrings will be the result

The code is running when I give a text file wih only two words. But when there are more than two words I am getting following error

java.lang.StringIndexOutOfBoundsException: String index out of range: 3

I feel the error is occuring in this line 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);

}
    }

解决方案

This is the problem:

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

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.

这篇关于java.lang.StringIndexOutOfBoundsException:String索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆