Hi, thanks for the schooling. I did realize that the algorithm was a bit off and revised my code, I just didn't post the revised code:
import java.util.ArrayList;
public class Sentence
{
public Sentence( String newWord )
{
word = newWord;
}
public boolean find( String find )
{
if( word.length() == find.length() && !(word.substring( 0, find.length() ).equalsIgnoreCase(find) ) )
{
result = false;
}
else if( !(word.substring( 0, find.length() ).equalsIgnoreCase(find) ) )
{
String shorterWord = word.substring( 1, word.length() );
Sentence shorterSentence = new Sentence( shorterWord );
result = shorterSentence.find(find);
}
else if( word.substring( 0, find.length() ).equalsIgnoreCase(find) )
{
result = true;
}
return result;
}
private boolean result;
private String word;
}
I still think that the third else if cause (the first one in the revised code above) is necessary. For example, if the word was "robot" and we were searching for "bot":
(robot, bot) = (obot, bot)
(obot, bot) = (bot, bot)
If I took out that else if clause, word.length() would surely equal find.length() but it wouldn't check to see whether the words matched and would just denote to false. Adding !(word.substring(0, find.length() ).equalsIgnoreCase(find) ) ) allows it to check for equality (or non-equality in this case). That's the only way I could think of to get it to skip over this step if the words matched and go into the last else if clause (the one that sets result to true).
Is my logic off here?