Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Have a program which I'd like output the values from an Array. Using tab (\t) or formatting e.g (%40s) doesnt account for strings of different lengths.

Current Code Below is Fine for the Headings.

    System.out.format("%s%40s%32s%32s%32s%32s\n","ID","NAME","SURNAME","POSITION","CONTACT","EMAIL");

However, Not for the values of different Lenths

I'd Like something like this below ***

Name         Surname         ID           Position
Joanne       Bassy           0000001      Manager
Mannnny      papsasjkasjnnxz 0000002      Boss

I Get this though ***

Name         Surname         ID           Position
Joanne       Bassy           0000001      Manager
Mannnny    papsasjkasjnnxz 0000002         Boss
share|improve this question
    
could be possible only by using Monospaced Fonts and to use \t, –  mKorbel Jul 23 '13 at 11:36
4  
use JTable instead of bothering with and by remove lines in columns and rows (JTable API) –  mKorbel Jul 23 '13 at 11:38
    
thanks for your response mKorbel –  ABURU 2011 Jul 23 '13 at 11:44

1 Answer 1

Change your format string as follows:

  1. Add a width specifier to the first format specifier to specify the width of the first column
  2. Add a - flag on all the format specifiers to left align the values

The updated format string would be "%-40s%-40s%-32s%-32s%-32s%-32s\n".

See the Format String Syntax for more details.

Note that this solution has several limitations:

  • It will only work when you use a fixed width font
  • If any string is longer than the provided width, it will push the rest of the line out and the columns won't line up.
  • The user will not be able to easily manipulate the data (column width, column order, sort order)
  • The user won't be able to easily copy the output and paste directly into a spreadsheet.

Alternatives include

  • Using a JTable, which would be more flexible but this would require more coding.
  • Using \t to separate the fields instead of padding with spaces. This option would allow copying into a spreadsheet, but still has the other limitations and may be more difficult to line up the column values if they are not all roughly the same length.
share|improve this answer
1  
Assumes a fixed width font. –  trashgod Jul 23 '13 at 14:28
    
Yes, I assumed a fixed width font as that is how the example was given. As @mKorbel notes, the best would be to use a JTable but if there is a restriction to using only text output this solution will work in conjuntion with a fixed width font. –  Rangi Keen Jul 23 '13 at 15:56
    
+1 for addressing the question directly, but it wouldn't hurt to expand on the limitations and alternatives. –  trashgod Jul 23 '13 at 16:07
    
There arent any Limitations however, prefernce would be to use a text output option. Thanks Rangi Keen / trashgod for yoyur Input –  ABURU 2011 Jul 24 '13 at 0:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.