Java - Write code to Return a number of spaces that is proportional to the argument.

Requirements

Write code to Return a number of spaces that is proportional to the argument.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        int level = 42;
        System.out.println(getIndentPrefix(level));
    }/*from  ww  w.j  a v  a 2  s . c  o m*/

    /** Return a number of spaces that is proportional to the argument.
     *  If the argument is negative or zero, return an empty string.
     *  @param level The level of indenting represented by the spaces.
     *  @return A string with zero or more spaces.
     */
    public static String getIndentPrefix(int level) {
        if (level <= 0) {
            return "";
        }

        StringBuffer result = new StringBuffer(level * 4);

        for (int i = 0; i < level; i++) {
            result.append("    ");
        }

        return result.toString();
    }
}

Related Exercise