开发者

Adding dots into a system.out.println

well basically I have a line thats has a finial in of 40, if a add 2 words that are 10 letters each long that leaves me with 20 spaces thouse 20 spaces should be filled with dots ...... for example hello................................you

or

stack...........................overflow

now I have the code working fine, I just have no idea on how to please this in a system.out.println so that the result is printed to the console.

I was thinking o开发者_如何转开发f using a whileloop to print the dots one at a time, after testing how many number of dots there should be once bother words are entered.

At the moment I have this which clearly doesn't work

{
       System.out.println (word1 + ".." + word2);

       while (wordlegnth1 + wordlegnth2 < LINELENGTH)

       {
           System.out.println (word1 + "." + word2);

           wordlegnth1++;

       }


final int LINE_LENGTH = 40;

String word1 = ...;
String word2 = ...;

StringBuilder sb = new StringBuilder(LINE_LENGTH);
sb.append(word1);
for (int i = 0; i + word1.length() + word2.length() < LINE_LENGTH; i++) {
    sb.append(".");
}
sb.append(word2);

System.out.println(sb.toString());

Note: the use of StringBuilder is to avoid performance penalty due to String immutability


/**
 * Formats a given string to 40 characters by prefixing it with word1 and 
 * suffixing it with word2 and using dots in the middle to make length of final
 * string = 40.
 * If string will not fit in 40 characters, -1 is returned, otherwise 0 is
 * returned.
 * 
 * @param word1 the first word
 * @param word2 the second word
 * @return 0 if string will fit in 40 characters, -1 otherwise
 */
public static int formatString(String word1, String word2) {
    final int LINELENGTH = 40;

    // determine how many dots we need
    int diff = LINELENGTH - (word1.length() + word2.length());

    if (diff < 0) {
        // it's too big
        System.out.println("string is too big to fit");
        return -1;
    }
    // add the dots
    StringBuilder dots = new StringBuilder();
    for (int i = 0; i < diff; ++i) {
        dots.append(".");
    }

    // build the final result
    String result = word1 + dots.toString() + word2;

    System.out.println(result);

    return 0;

}

public static void main(String[] args) throws Throwable {
    formatString("stack", "overflow");
    String str = "";
    for (int i = 0; i < 21; ++i) {
        str += "a";
    }
    formatString(str, str);
}

Output:

stack...........................overflow
string is too big to fit


Why not start with something like:

int lengthA = word1.length();
int lengthB = word2.length();
int required = LINE_LENGHT - (lengthA + lengthB);

for(int i = 0; i < required; i++)
{
    System.out.print(".");
}

It can be improved on (try strings that are very large for example, or use a StringBuilder rather than System.out.print).


add apache's commons-lang to your classpath and use StringUtils.leftPad()

System.out.println(str1 + StringUtils.leftPad(str2, 40 - str1.length(), '.'));

why write a method to do something someone else has already written + tested?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜