Java - Write code to Repeats the given String repetitions number of times.

Requirements

Write code to Repeats the given String repetitions number of times.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String stringToRepeat = "book2s.com";
        int repetitions = 42;
        System.out.println(repeater(stringToRepeat, repetitions));
    }/*from www.j  a v a  2s  .  co m*/

    /**
     * Repeats the given {@link String} {@code repetitions} number of times.
     * 
     * @param stringToRepeat
     *            The {@link String} to repeat.
     * @param repetitions
     *            Number of times to repeat it.
     * @return A {@link String} holding the repetitions.
     */
    public static String repeater(String stringToRepeat, int repetitions) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < repetitions; i++) {
            result.append(stringToRepeat);
        }
        return result.toString();
    }
}

Related Exercise