Reputation: 1
char d;
String e="",i1="",d1="";
public static void main(String args[])throws IOException
{
String str="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string");
str=br.readLine();
int l,i=0;
l=str.length();
char k;
String f="";
Vivek obj=new Vivek();
while(i<l)
{
k=str.charAt(i);
f=Character.toString(k);
System.out.println(k);
obj.recursive(str,f,i+1,l);
i++;
}
}
public void recursive(String s,String c,int i,int l)
{
for(int j=i;j<l;j++)
{
d=s.charAt(j);
d1=Character.toString(d);
System.out.println(c+d1);
}
//System.out.println();
i1=Character.toString(s.charAt(i));
e=c+i1;
//System.out.pritnln(e);
recursive(s,e,i+1,l);
}
The Output of the program is if string abcd is input
a
ab
ac
ad
abc
abd
abcd
it then print out the error
StringIndexOutOfBounds
...Can u tell me how to make it right
Upvotes: 0
Views: 499
Reputation: 18123
You don't have any break condition in your recursive function, so it runs like forever - not fully understanding your code, I would propose something like the following as the first line of recursive
: if (i == l) { return; }
.
Upvotes: 2