Using Recursion to Replace Parts of One String with Another (Java)
I need to write a method that replaces every "F" in the string initiator with the string generator (using recursion). Here's my shot at it, but it doesn't seem to be working properly:
public String nextGeneration(String initiator, String generator)
{
String result2 = "";
if (initiator=="F")
{
result2 = generator;
}
else
{
if (initiator.substring(0,0)=="F")
{
result2 = generator + nextGeneration(initiator.substring(1,initiator.length()), generator);
}
else if (initiator.substring(0,0)=="+")
{
result2 = "+" + nextGeneration(initiator.substring(1,initiator.length()), generator);
}
else if (initiator.substring(0,0)=="-")
{
result2 = "-" + nextGeneration(initiator.substring(1,initiato开发者_如何学编程r.length()), generator);
}
}
return result2;
}
Any suggestions?
Don't use ==
for comparing Strings, use string1.equals(string2)
.
string1 == string2
tests if the two references refer to the same object in memory, not if their string content values are equal.
Your question is missing information as you have other characters you are reacting to that you don't explain about.
I was playing with your code to see how it may be working, and I realized when I added the initiator.length() == 0
bit that there is a problem with your code.
I am leaving my attempt here, as it may help you see what you did differently, but it isn't correct.
In a recursive program it helps if you walk through it with pencil/paper to see what happens, but, basically, your first if
statement is flawed as it is looking for the iterator to be an "F"
which isn't what you want, but, if it isn't one of these three characters it will return, quitting early, which is probably what you don't want.
So, walk through it with an example, and write down what you expect will happen, then go through your logic and see what happens, and then update your question with the results and your analysis of what went wrong.
But, after going through it by hand, then use a debugger to walk through to see if you can determine how to fix your program.
public String nextGeneration(String initiator, String generator)
{
String result2 = "";
if (initiator.length() == 0) {
return initiator; // This is where I realized you have a problem, I couldn't see what should be returned here.
}
char f = initiator.charAt(0);
switch(f) {
case 'F':
result2 = generator + nextGeneration(initiator.substring(1,initiator.length()), generator);
break;
case '+':
result2 = "+" + nextGeneration(initiator.substring(1,initiator.length()), generator);
break;
case '-':
result2 = "-" + nextGeneration(initiator.substring(1,initiator.length()), generator);
break;
default:
break;
}
return result2;
}
public static String nextGeneration(String initiator, String generator){
StringBuffer result = new StringBuffer();
if(initiator.length() > 0){
char letter = initiator.charAt(0);
if(letter == 'F')
result.append(generator);
else
result.append(letter);
result.append(nextGeneration(initiator.substring(1),generator));
}
return result.toString();
}
精彩评论