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   www  . ja v  a 2s . c  om*/

  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