java.lang.StringIndexOutOfBoundsException:String索引超出范围 [英] java.lang.StringIndexOutOfBoundsException: String index out of range
问题描述
当我给一个文本文件只有两个字时,代码正在运行。但是当有两个以上的单词我得到以下错误
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屋!