Java - Write code to set String Length to shorten string

Requirements

Write code to set String Length to shorten string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "book2s.com";
        int howLong = 42;
        System.out.println(setStringLength(s, howLong));
    }// www  .j a va  2  s .  c o  m

    public static String setStringLength(String s, int howLong) {
        String result;
        if (s.length() > howLong) {
            result = s.substring(0, howLong - 3) + "...";
        } else if (s.length() == howLong) {
            result = s;
        } else {
            result = createSpaces(howLong - s.length());
            result += s;
        }
        return result;
    }

    public static String createSpaces(int howMany) {
        String result = "";
        for (int n = 0; n < howMany; n++) {
            result += " ";
        }
        return result;
    }
}