I'm supposed to write a simple method that returns given string for given amount, seperated by comma (and no comma in the end), with recursion. If there are less than two counts, the return is empty string "".
final static String COMMA = ", ";
public static String replicate(String s, int count) {
String answer = "";
if (count < 2) {
return answer;
}
else {
answer = s + COMMA + replicate(s, (count - 1));
return answer;
}
}
If I put s = cat and count = 5, I get cat, cat, cat, cat,- One short what I need.
I'm at my wits end here what to do, to get proper amount of repeats here without the comma at the end.
EDIT: Clearly I failed to communicate, that the method SHOULD return an empty string, if the count is two or less. Sorry for the lack of clarity there.
String answer = s;...