recursion

//this method will remove the text between [ and ], the input string may contains more than 1 pair of [].

it doesn't work, please let me know what is wrong in this recursion method. thanks

public static String remove_text(String text) throws IOException{

   if(text.length()>0){
       
       String text_after = null;
       if(text.contains("[")&&text.contains("]")){
           int begin_index =text.indexOf("[");
           int end_index =text.indexOf("]");
           
           text_after = text.substring(0,begin_index-1) + text.substring(end_index+1, text.length()-1);          
           remove_replacement_text(text_after);
       }
       
       return text_after;
   }
return text;
keepworkingAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
thing your returns also aren't quite right, try:


public static String remove_text(String text) throws IOException{

   if(text.length()>0 && text.contains("[")&&text.contains("]")){
       
       String text_after = null;
       int begin_index =text.indexOf("[");
       int end_index =text.indexOf("]");
           
       return remove_text(text.substring(0,begin_index) + text.substring(end_index+1, text.length()));    
   }
   return text;
}
0
 
objectsCommented:
theres no recursion in what you've posted (and it doesn't look like it'll compile either)
whats this method do

           remove_replacement_text(text_after);

you need to assign the value by the looks

           text_after = remove_replacement_text(text_after);
0
 
keepworkingAuthor Commented:
objects:

thanks first.

it compiles, I did try text_after = remove_replacement_text(text_after); it doesn't work.


 remove_text(String text) will remove the text between [ and ], for example, text is:
1 [1] [2] [3]
after, should be:
1
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
asood314Commented:
you should call remove_text(text_after) instead of remove_replacement_text(text_after).
0
 
objectsCommented:
>  remove_text(String text) will remove the text between [ and ], for example, text is:

your calling remove_replacement_text, *not* remove_text
0
 
keepworkingAuthor Commented:
asood314:

I did call remove_text(text_after) , that is typo. :-(
0
 
objectsCommented:
>            text_after = text.substring(0,begin_index-1) + text.substring(end_index+1, text.length()-1);          

and that should be:

           text_after = text.substring(0,begin_index) + text.substring(end_index+1, text.length());          
0
 
asood314Commented:
what output are you getting?
0
 
keepworkingAuthor Commented:
my output is:

1 [2] [3
0
 
asood314Commented:
objects is right; it should be text.length() not text.length() -1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.