Java - Write code to generate String Of Length with for loop

Requirements

Write code to generate String Of Length with for loop

Demo

public class Main {
  public static void main(String[] argv) {
    int length = 42;
    System.out.println(generateStringOfLength(length));
  }/*from   w  ww .jav  a 2 s . co  m*/

  public static String generateStringOfLength(int length) {
    return generateStringOfLength(length, 'a');
  }

  public static String generateStringOfLength(int length, char character) {
    assert (length >= 0);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++) {
      sb.append(character);
    }
    return sb.toString();
  }

}

Related Example