4

This has been giving me a headache all day and I cannot figure it out. The goal is to have the string repeat itself using the times param as the number of times the string is able to repeat its self. for example:

stringTimes("Hello", 3); //should return HelloHelloHello,
stringTimes("cat", 2); //should return catcat, 
stringTimes("monkey", 0); //should return _____,

below is the code I've been using and I'm getting nothing. HELP!!!

public static String stringTimes(String theString, int times)
{   
    String adder = "";
    if (times >= 1) {
        adder += theString;
        return stringTimes(theString, times - 1);
    }
    return adder;
}   

public static void main(String[] args) {
    System.out.println(stringTimes("hello ", 8 ));
}
3
  • 2
    you need to pass in "adder" to the internal call to stringTimes (instead of the original string, theString) Commented Jul 12, 2016 at 22:00
  • If this is a class assignment it is a very bad one. Recursion is the wrong tool for this job. If you were teaching a class on auto mechanics would you ask students to tighten lug nuts with a small pair of pliers, "just as an exercise"? Commented Jul 12, 2016 at 22:17
  • yes, but then again there is another assignment later on down the line that requires that I use a for loop Commented Jul 12, 2016 at 22:18

2 Answers 2

3

Your method is going down to the last recursive call and then just returning an empty string. Change it to:

public static String stringTimes(String theString, int times)
{   
    if (times >= 1) {
        return theString + stringTimes(theString, times - 1);
    }
    return "";
}   
Sign up to request clarification or add additional context in comments.

9 Comments

adder is hardly necessary, perhaps write an else condition that returns an empty string? That might illustrate recursion better.
@TimStraubinger Yeah you're right, that's cleaner. Edited.
@KeithBryant sure, the cleanest way to write this doesn't involve recursion. Do you want to see that way or are you looking for the cleanest way using recursion?
@KeithBryant Using recursion this is about the cleanest it is going to get. You were very close to the best answer on your own, you were just missing one piece to make the recursive call work.
@KeithBryant nobody's said it outright yet, but the cleaner way to write this is via a loop that counts its iterations. Of course, that doesn't involve recursion, but I think everyone will agree that it is cleaner.
|
0

Here a simple and compressed one:

public static String stringTimes(String theString, int times) {
    return times > 0 ? theString + stringTimes(theString, times - 1) : "";
}

Advantages:

  • Only one line with a simple ternary operator.
  • In my opinion, one return statement is easier to read in this particular case.

1 Comment

This is correct but I don't know why you would write it like this - it simply makes it harder to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.