The task is to compress a string.eg. abcbcdabcbcdHere as you can see some characters are repeated, so it can be compressed.abcbcdabcbcd -> (a(bc2)d2)$'$' denotes end of string.My code:import java.util.*;class compress{    public static void main(String[] ar)    {            System.out.print(>> );            Scanner sc = new Scanner(System.in);        String s = sc.next();        System.out.println(compress(s));    }    public static String compress(String s)    {            String a = ;            boolean found = false;            for(int l = 0; l < s.length(); l++)            {                for(int i = l; i < s.length(); i++)                {                    String t = s.substring(l,i+1);                    try                    {                        int q = 1;                        int st = i+1;                        String p = ;                        while(st + t.length() <= s.length())                        {                            p = s.substring(st,st+t.length());                            if(p.equals(t))                                q++;                            else break;                            st += t.length();                        }                        if(q != 1)                        {                            a += ( + compress(t) + q + );                            l = st-1;                            i = st;                            found = true;                        }                   }catch(Exception e){}                }                if(!found)                    a += s.charAt(l);                found = false;        }        return a;    }}Please help to improve my code and suggest a better solution.
Compressing a string
java;strings
import.util.*One thing I have notice from reading other Java Reviews is that you don't want to import more libraries than you have to, really this goes for any language I would think.  so figure out what you need in java.util and import that. I know that in C or C++ there is a library that when imported really annoys other programmers, but I can't remember what it is,  not sure if there is a similar library in Java though.If you aren't going to do anything with the exception don't catch it.  don't use a Try ... Catch let it tell you what you are doing wrong every time so that you can code it right.  this should give you a little bit of performance.not sure what type of competitive programming that you are doing, but in some competitions they go by number of lines so removing the Try Catch would save you lines.From my Commentshouldn't (a(bc2)d2)$ be something like ((a(bc2)d)2)$ ? logically I would think that you want the pattern a(bc2)d to repeat twice, not have d repeat twice at the end.I hope that makes sense.Eliminate an If Statementthis code here can be changedwhile(st + t.length() <= s.length()){    p = s.substring(st,st+t.length());    if(p.equals(t))        q++;    else break;    st += t.length();}if(q != 1){    a += ( + compress(t) + q + );    l = st-1;    i = st;    found = true;}to something like this:while(st + t.length() <= s.length()){    p = s.substring(st,st+t.length());    if(p.equals(t))    {        q++;        a += ( + compress(t) + q + );        st += t.length();        l = st-1;        i = st;        found = true;    }    else break;}because if you increment q it will not be equal to 1, and the rest of the logic stays the same,  this should also increase the performance.another thing that I just noticed, and this may go against the whole competitive programming thing, and may make the code more unreadable but you are  adding two variables together the same way multiple times.create another variable perhaps stt and use that to represent st+t.length  then you can write it like this.int st = i+1;int stt = st + t.lengthwhile(stt <= s.length()){    p = s.substring(st,stt);    if(p.equals(t))    {        q++;        a += ( + compress(t) + q + );        l = st-1;        i = stt;        found = true;    }    else break;}Here we removed an assignment and redundancy by creating another variable called stt (whatever that is)