1

What I mean, is that I have a simple 2x2 matrix that I want to print out to a JTextArea, however this matrix uses numbers that uses varying integer lengths, and I don't know what the lengths will be prior to the program executing. This means when I try to neatly format the output string it'll sometimes end up being untidy.

For example, I have a matrix like the one below:

     | ham | spam |
ham  |  x  |   y  |
spam |  z  |   a  |

However, depending on the length of the integers the format will change. Say if x was equal to 23, the whole row would be pushes along one space so it'll look untidy, like so:

     | ham | spam |
ham  |  23  |   y  |
spam |  z  |   a  |

I was just wondering if there was any way to avoid this, aside from using a lot of if statements and trial/error to get the string sizes correct. I realise it's purely an aesthetic thing. It's not crucial to my program's operation. I just like things looking good.

  • Why not use `mystring.length()` to know its size, and display a number of `space` characters depending on the string size? (the bigger the string size, the smaller the amount of spaces in each cells). Or you can set your cell size equals to the maximum of all your strings' length, +2 (one space before, one space after), by right-padding. – Vulpo Dec 22 '17 at 13:13
  • I ended up using string.length() and a lot of if statements, but it's working now. So thanks! –  Dec 22 '17 at 13:36

1 Answers1

1

You can use String.format() or its print-equivalent System.out.printf()

System.out.printf("%-10s | %5s | %s \n", "ham", 23, "y");
System.out.printf("%-10s | %5s | %s \n", "spam", "z", "y");
System.out.printf("%-10s | %5s | %s \n", "stamp", 1984, "y");

prints

ham        |    23 | y 
spam       |     z | y 
stamp      |  1984 | y 

The magic here is done by %s which says "format the argument as a string" which is parametrized with a number that tells it the minimum length of the string. Meaning, if the given String is shorter than that number, it will be padded with spaces. The first %s is also parametrized with a - which means "make the string left-justified"

You can learn more about Formatter syntax on the javadoc page

To find the right number for your format-string, you would need to go over all your rows before printing to find out the maximum length.

Michael A. Schaffrath
  • 1,992
  • 1
  • 14
  • 23
  • This is a much more concise solution to what I just wrote. I might just go back and replace everything using printf instead. Thanks for this! –  Dec 22 '17 at 13:37
  • Just another quick question, do you know if JTextArea.append() removes certain whitespace? I have an output in my console and it looks fine and then the JTextArea that prints the same string makes the output look weird, like in this gist: https://gist.github.com/anonymous/f973ebf00b4fb182589f40f597c6cf2d Edit: Or actually it might be to do with the JTextArea not having regular spacing for its characters? –  Dec 22 '17 at 13:39
  • Yes, that's most likely a font issue. In "normal" fonts each character has it's own width, so that text is easier to read. E.g. a "l" is much narrower than a "m". In coding, you usually use *monospaced* fonts, which have the same width for all characters. – Michael A. Schaffrath Dec 22 '17 at 13:44
  • So for your table to be displayed correctly, you'll have to set a monospace font to the JTextField. Have a look at [this question](https://stackoverflow.com/questions/16279781/getting-jtextarea-to-display-fixed-width-font-without-antialiasing) to see how to do this. – Michael A. Schaffrath Dec 22 '17 at 13:48