I have to use recursion to implement a boolean method. No any for loop is allowed. The code I wrote turns out the right answer as given. However, it is not correct yet. Is there any good suggestion? Thanks!
public class RecusiveMethod {
public static void main ( String[] args ) {
System.out.println( "True: " + isWordCountsRight( "ccowcow", "cow", 2 ) );
System.out.println( "True: " + isWordCountsRight( "kayakayakaakayak", "kayak", 3 ) );
}
public static boolean isWordCountsRight( String str, String word, int n ) {
if ( n == 0 ) return true;
if ( str.substring( 0, word.length() ).equals( word ) ) {
return isWordCountsRight( str.substring( 1 ), word, n - 1 );
}
return isWordCountsRight( str.substring( 1 ), word, n );
}
}
false- guess that is your problem here