?
Solved

recursion

Posted on 2007-07-24
10
Medium Priority
?
170 Views
Last Modified: 2012-05-05
//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;
0
Comment
Question by:keepworking
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 19561486
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
 

Author Comment

by:keepworking
ID: 19561511
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
 
LVL 3

Expert Comment

by:asood314
ID: 19561520
you should call remove_text(text_after) instead of remove_replacement_text(text_after).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 19561550
>  remove_text(String text) will remove the text between [ and ], for example, text is:

your calling remove_replacement_text, *not* remove_text
0
 

Author Comment

by:keepworking
ID: 19561554
asood314:

I did call remove_text(text_after) , that is typo. :-(
0
 
LVL 92

Expert Comment

by:objects
ID: 19561561
>            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
 
LVL 3

Expert Comment

by:asood314
ID: 19561573
what output are you getting?
0
 

Author Comment

by:keepworking
ID: 19561591
my output is:

1 [2] [3
0
 
LVL 3

Expert Comment

by:asood314
ID: 19561601
objects is right; it should be text.length() not text.length() -1
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 19561618
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question