im trying to do find the all the substrings in a string,i have written the following codes, but i have some unwanted outputs as you see below: the method first print the substring (0,1) then it calls itself by incrementing b with 1 and keep going like this, when b>string's length, it will preincrement a,and passes a+1 to the b, and it continues like this, until the last substring where a+1==string's length, this when my recurive program should terminate.
public static void main(String[] args) {
recsub("java",0,1);
}
public static void recsub(String str,int a,int b){
if(b>str.length()) {
System.out.println();
recsub(str,++a,a+1);
}
else {
System.out.print(str.substring(a,b)+" ");
}
if((a+1)==str.length()) {
}
else {
recsub(str,a,b+1);
}
The output from this code is:
j ja jav java
a av ava
v va
a
a
v va
a
a
or Can I break out through this method and return to the main, whenever the program enters that if (if(a+1)==...), like breaking a loop?