Reputation: 189
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);
}
}
Upvotes: 3
Views: 62344
Reputation: 7579
When you use string.charAt(x), you must check that it is not beyond string length. Documentation shows that you will get "IndexOutOfBoundsException if the index argument is negative or not less than the length of this string". And in your particular case, you are only validating in the loop that you are under w length, so it will fail.
As SO already said, the loop runs only taking into account w length, so in case you have a shorter s, it will raise that exception. Check the condition so it goes up to the shorter string or rethink the process.
Upvotes: 1
Reputation: 5565
A few tips:
First, always include the full stack trace when you are asking for debugging help. It should point to the exact line number the issue is happening on.
Second, your issue is likely in your most inner loop for(int j1=0;j1<w.length();j1++,i1++)
you are incrementing i1
in addition to j1
this will cause i1
to eventually go beyond the size of String s
Finally, you should consider using the String.contains() method for Strings or even a Regular Expression.
Upvotes: 1
Reputation: 1500923
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:
String.regionMatches
Upvotes: 6