0

I'm printing out a list of things and I formatted the text with spaces. but instead of spaces I just want all dashes.... I cant figure out or find a way to do this and at the same time keep the text formatted correctly.

here is my format code:

System.out.println(String.format("Word:%11s-----------Hash Value:%5s ",arrayString[i],arrayNums[i]));

here is an example output:

Word:         DO-----------Hash Value:    2 
Word:        END-----------Hash Value:    3 
Word:       ELSE-----------Hash Value:    4 
Word:       CASE-----------Hash Value:    5 
Word:     DOWNTO-----------Hash Value:    6 
Word:       GOTO-----------Hash Value:    7 
Word:         TO-----------Hash Value:    8 
Word:  OTHERWISE-----------Hash Value:    9 
Word:       TYPE-----------Hash Value:   10 
Word:      WHILE-----------Hash Value:   11 
Word:      CONST-----------Hash Value:   12

Desired output:

Word:---------DO-----------Hash Value:----2 
cmehmen
  • 249
  • 1
  • 3
  • 12
  • As a workaround you could replace spaces with dashes after formatting. Take care of the space in "Hash Value", though. Not sure if there is a built-in way. – Marvin Apr 26 '15 at 22:22
  • http://stackoverflow.com/questions/3450758/string-format-to-fill-a-string – Sean F Apr 26 '15 at 22:24

3 Answers3

1

This will work.

String stringToPrint = String.format("Word:%11s-----------Hash Value:%5s ",arrayString[i],arrayNums[i]);
stringToPrint = stringToPrint.replace(' ', '-');
System.out.println(stringToPrint);
Brendan
  • 136
  • 1
  • 10
  • 1
    This will also replace the space in "Hash Value". – Marvin Apr 26 '15 at 22:25
  • One minor inconvenience is that this will also replace the space in the "Hash Value" label. If that's acceptable, then this may be the most straightforward solution.. – Michael Burr Apr 26 '15 at 22:25
  • The way it is worded it seems like he wants all dashes instead of spaces. Will look into the other case. – Brendan Apr 26 '15 at 22:30
  • According to his "desired output" I'm rather sure that he does not want to replace the space in "Hash Value" ;) – Marvin Apr 26 '15 at 22:34
0

You could do

System.out.println(String.format("Word:%11s-----------Hash Value:%5s ".replace(" ", "ƒ"),
        arrayString[i], arrayNums[i]).replace(" ", "-").replace("ƒ", " "));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

Something like this?

public class Dash
{
  public static void main (String[] args)
  {
    String dashes = "-------------";
    String[] verbs =
        { "DO", "END", "ELSE", "CASE", "DOWNTO", "GOTO", "TO", "OTHERWISE", "TYPE",
         "WHILE", "CONST" };

    for (String verb : verbs)
      System.out.printf ("%s%s%s%n", dashes.substring (verb.length ()), verb, dashes);
  }
}

which gives

-----------DO-------------
----------END-------------
---------ELSE-------------
---------CASE-------------
-------DOWNTO-------------
---------GOTO-------------
-----------TO-------------
----OTHERWISE-------------
---------TYPE-------------
--------WHILE-------------
--------CONST-------------
dmolony
  • 1,125
  • 9
  • 23