• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting the way Array Information is displayed  RSS feed

 
Seth Lamb
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my output:


Day AM PM
--------------------------------
Sat
Sun
Mon
Tues
Wed
Thurs
Fri
5 1 1 2 4 1 0

Basically I want to align the AM traffic incidents under the heading "AM" but am not sure how to accomplish this. I am using separate arrays for all three of these types of information and need the info to line up with their respective titles.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two problems which I think are separate. One is using separate arrays, a non‑object‑oriented technique, and also error‑prone. If you can create a class which encapsulates accidents by day then you can do the formatting in its toString method.
I would suggest you use the % tags to format your output. You can read about % tags in the Java® Tutorials with ctrl‑F‑formatting. This is one of the likely hits. You can fill a space for the number and specify how wide that space will be; then all the numbers in the different columns will align. You would use
System.out.printf("%x%y%z", something, somethingElse, somethingDifferent);
… only those are not real % tags. The tutorials links will tell you the commonly used tags.
If you work out the % tags for the different columns, you can use the same format in the toString method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!