0

I would like to print out a list of items with a row of periods, or ellipsis, between the first text and final variable with a space on each side. So if the spacing is three or more, there should be a period(s) taking place of the spaces in between. I would like to do this inside of a printf, if possible; using Formatting. I know if I put a 0 in front of the width as a flag, then the extra characters will be taken up as zeros, but I'd like the zeros to be as decimal points instead.

Is it possible to do it this way?

Sample Code:

    System.out.printf("Egg Count: %9d%n", 300); //list.getNumberOfEggs()
    System.out.printf("Chicken Count: %5d%n", 1378); //list.getNumberOfChickens()
    System.out.printf("Corn Oil Count: %4d%n", 619); //list.getNumberOfCornOil()
    System.out.printf("Orange Count: %6d%n", 2000); //list.getNumberOfOranges()

Sample Output:

Egg Count:       300
Chicken Count:  1378
Corn Oil Count:  619
Orange Count:   2000

Desired Output:

Egg Count: ..... 300
Chicken Count:  1378
Corn Oil Count:  619
Orange Count: . 2000

I found JS - Separating a string with periods to meet certain character length which is somewhat similar, but not what I'm looking for with the use of formatting.

I also found Change the default padding character in Java printf? which can be used for replacing the spaces to demicals, but only if the above printf is split after the colon. This also does not address the spaces to be placed after the colon and before the first digit; perhaps this may be done with Regex/Regular Expressions somehow?

2learn
  • 1

2 Answers2

0

Use String.repeat(int times) to repeat the . character based on the number of digits.

System.out.printf("Egg Count: %s %d%n",".".repeat(8-String.valueOf(300).length()), 300); //list.getNumberOfEggs()
System.out.printf("Chicken Count: %s %d%n",".".repeat(4-String.valueOf(1378).length()), 1378); //list.getNumberOfChickens()
System.out.printf("Corn Oil Count: %s %d%n",".".repeat(3-String.valueOf(619).length()), 619); //list.getNumberOfCornOil()
System.out.printf("Orange Count: %s %d%n",".".repeat(5-String.valueOf(2000).length()), 2000); //list.getNumberOfOranges()
Ashutosh
  • 2,827
  • 2
  • 12
  • 11
0

You can use StringBuilder to pad the desired character.

public static String padLeftCharacter(int inputInteger, int length, String padChar) {
    String inputString = " " + inputInteger;
    if (inputString.length() >= length) {
        return inputString;
    }
    StringBuilder sb = new StringBuilder();
    while (sb.length() < length - inputString.length()) {
        sb.append(padChar);
    }
    sb.append(inputString);

    return sb.toString();
}
System.out.println("Egg Count: " + padLeftCharacter(300, 9, "."));
System.out.println("Chicken Count: " + padLeftCharacter(1378, 5, "."));
System.out.println("Corn Oil Count: " + padLeftCharacter(619, 4, "."));
System.out.println("Orange Count: " + padLeftCharacter(2000, 6, "."));

Result

Egg Count: ..... 300
Chicken Count:  1378
Corn Oil Count:  619
Orange Count: . 2000
ruvenij
  • 188
  • 6