-1

I tried the following and it fails to output anything. I would ideally like to have the String text repeated n number of time by int multiplier. What am I doing wrong?

String text and int multiplier are passed as arguments from another method.

public static String repeatText(String text, int multiplier)
{
  String value = "";
  StringBuilder repeat = new StringBuilder(text.length() * multiplier);
  repeat(repeat, text, multiplier);

  value.equals(repeat);

  System.out.println("Text Repeated:");
  System.out.println("-----------");
  System.out.println(repeat);
  System.out.println("--------------");

  return value;
}
piet.t
  • 11,718
  • 21
  • 43
  • 52

3 Answers3

1

Try the following method:

 public static String repeat(String toRepeat, int times) {
    if (toRepeat == null) {
        toRepeat = "null";
    }

    if (times <= 0) {
        return "";
    }

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < times; i++) {
        sb.append(toRepeat);
    }

    return sb.toString();
}
ovunccetin
  • 8,443
  • 5
  • 42
  • 53
  • You don't have to validate your input. `append` tests for null, and if value of `times` is not positive loop will not execute making `st.toString()` return `""`. – Pshemo Feb 18 '14 at 07:18
1

user for loop

for(int i=1; i<=multiplier; i++)
{

your logic

}
Mahesh
  • 730
  • 7
  • 26
0
 public static void repeat(StringBuilder repeat,String text, int multiplier){

    for(int i=0; i<multiplier; i++){
        repeat.append(text);
    }
}
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44