Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7428935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:53:23+00:00 2026-05-29T08:53:23+00:00

Hi I wrote a java code to find longest word made of other words.

  • 0

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);

}
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:53:24+00:00Added an answer on May 29, 2026 at 8:53 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code to read a file in my Java Servlet class. (I'm
Here I wrote Java code to display duplicate numbers in a text file. Here
I have this Java code some other person wrote, specifically JSPs. I am trying
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
Is there any maximum size for code in Java? I wrote a function with
I wrote a Java program to add and retrieve data from an MS Access.
I am new to Java and wrote this code. It has a simple class
I'm trying to run this code taken from Sun Java site (I didn't copy
I'm trying to call the java code from S. This method call: cls =
I wrote this code to find all possible permutations of some numbers. But i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.