Link to home
Start Free TrialLog in
Avatar of keepworking
keepworking

asked on

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;
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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);
Avatar of keepworking
keepworking

ASKER

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
you should call remove_text(text_after) instead of remove_replacement_text(text_after).
>  remove_text(String text) will remove the text between [ and ], for example, text is:

your calling remove_replacement_text, *not* remove_text
asood314:

I did call remove_text(text_after) , that is typo. :-(
>            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());          
what output are you getting?
my output is:

1 [2] [3
objects is right; it should be text.length() not text.length() -1
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial