0

My output looks like this

  000(05.00)      *|
  001(25.00)      ******|
  002(36.00)      **********|

Ideally I would like my output to be this....

  000(05.00)      *         |
  001(25.00)      ******    |
  002(36.00)      **********|

I have this as my variable...

  private static String MAX_REP = "|";

What can I do to MAX_REP to print that bar for every line at that same spot?

2 Answers2

1

Firstly, record each line as a String.

Then calculate the String's length. Add N-k spaces before MAX_REP when you print, where N is the position you want and k is the length of the given line.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
0

Without seeing your code, it's hard to know what you're doing, but from the looks of your output you want to fill in the number of spaces that are not occupied by stars up to some point holding |.

If you know the number of *s, then this is as simple as subtracting the number of stars numStars from total number of character positions numChars before |:

int numSpaces = numChars - numStars;

Then you add that many spaces. For example, using org.apache.commons.lang3.StringUtils:

String myStr = output + StringUtils.repeat("*", numStars) + StringUtils.repeat(" ", numSpaces) + "|";
user1205577
  • 2,388
  • 9
  • 35
  • 47