2
 public static String s1 = "team A won the match historically to clinch series which made surprise around the world ";
 public static String s2 = "He did his part";

 t1=s1.length();
 t2=s2.length();
 t3=Math.abs(t1-t2); 

  for(i=0;i<t3;i++)
  {
      Sub_string.add(s1.substring(i,t2++));
  }

How can I solve this?

4
  • Where is the exception being thrown? Commented Feb 4, 2011 at 6:25
  • 1
    What kind of object is sub_string? Commented Feb 4, 2011 at 6:26
  • actually Sub_string is a arraylist to which i add substrings. Commented Feb 4, 2011 at 6:36
  • 1
    Paste the exact program. This code works. Sub_string is [team A won the , eam A won the m, ...,around the worl, round the world] Commented Feb 4, 2011 at 6:48

3 Answers 3

1

tried it. doesn't seem to throw an exception

I guess you defined your list to have max size? if you define it like this it will work.

public class TestCode {
    public static String s1 = "team A won the match historically to clinch series which made surprise around the world ";
     public static String s2 = "He did his part";
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         ArrayList<String> Sub_string = new ArrayList<String>();  

        int t1=s1.length();
        int t2=s2.length();
        int t3=Math.abs(t1-t2); 


        try {
            for(int i=0;i<t3;i++)
              {
                  Sub_string.add(s1.substring(i,t2++));
              }
        } catch (Exception e) {
            System.err.println();

        }

    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

thank u for all of u for making a quick reply..this logic is correct.actually i made a mistake when specifying size of the list..thank u very much Aba Dov..
@KNsiva Glad I was able to answer your question :)
1

Code derived from your pseudocode, with the input strings you provided, will not throw any exception.

The exception you posted (StringIndexOutOfBoundsException: String index out of range: -1) is thrown when substring is called with beginIndex (in your case i) larger then endIndex (in your case t2).

You will also get a StringIndexOutOfBoundsException when you call your code with a s1 that is shorter then s2.

Comments

1

The following code would serve your purpose... gives no problem. Prints sum as 73 and a designer output..;) Hope its helpful!!!!

import java.util.ArrayList;
public class Trial {
    public static String s1 = "team A won the match historically to clinch series which made surprise around the world ";
     public static String s2 = "He did his part";

    public static void main(String[] args) {

         ArrayList<String> Sub_string = new ArrayList<String>();  

        int t1=s1.length();
        int t2=s2.length();
        int t3=Math.abs(t1-t2); 

        try {
            for(int i=0;i<t3;i++)
                  Sub_string.add(s1.substring(i,t2++));

        Object ia[] = Sub_string.toArray();
        for(int i=0; i<ia.length; i++)
            System.out.println(ia[i]);

        } catch (Exception e) {
            System.err.println();

        }

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.