1

i want to print some thing like this:

===================================================================================================

       Section 1              Section 2           Section 3
--------------------------------------------------------------------------------------------------
1110 - Ahmed Sami - Active         1116 - Maher Alsolami - Active    1122 - Waleed Alamri - Active
1111 - Mohammed Turkey - Active    1117 - Mohanned Sami - Active     1123 - Sami Alghamdi - Active
1112 - Sami Omer - Active          1118 - Feras Ahmed - Active       1124 - Majed Alharbi - Active
1113 - Bander Ali - Active         1119 - Nawaf Algarni - Active     1125 - Anwar Aljohani - Active
1114 - Basem Alzahrani - Active    1120 - Fahad Alharthi - Active    1120 - Fahad Alharthi - Active 
1115 - Turkey Almutairi - Active   1121 - Anas Batarfi - Active
==================================================================================================

but i get this :

===================================================================================================

       Section 1              Section 2           Section 3
--------------------------------------------------------------------------------------------------
1110 - Ahmed Sami - Active 1116 - Maher Alsolami - Active 
1111 - Mohammed Turkey - Active 1117 - Mohanned Sami - Active 
1112 - Sami Omer - Active 1118 - Feras Ahmed - Active 
1113 - Bander Ali - Active 1119 - Nawaf Algarni - Active 
1114 - Basem Alzahrani - Active 1120 - Fahad Alharthi - Active 
1115 - Turkey Almutairi - Active 1121 - Anas Batarfi - Active 
==================================================================================================

here is the code i use (the code does give an error in the end i just want to know how can sorted like the first table):

System.out.println("\nThe first distribution for students among the available sectios ");
System.out.println("===================================================================================================\n"
    + "\n"
    + "       Section 1           Section 2           Section 3\n"
    + "--------------------------------------------------------------------------------------------------");
for (int i = 0; i < 6; i++) {
   System.out.printf("%s%s%s%s", students[i].getStudID() + " - ", students[i].getFname() + " ", students[i].getLname() + " - ", students[i].getStatus() + " ");
   System.out.printf("%s%s%s%s", students[i + 6].getStudID() + " - ", students[i + 6].getFname() + " ", students[i + 6].getLname() + " - ", students[i + 6].getStatus() + " ");
   System.out.printf("%s%s%s%s", students[i + 12].getStudID() + " - ", students[i + 12].getFname() + " ", students[i + 12].getLname() + " - ", students[i + 12].getStatus() + " ");
   System.out.println("");
}
ollitietavainen
  • 3,900
  • 13
  • 30
  • 3
    read up on how printf formatting works. "%20s" will always print 20 characters. Start with that. That might be all you need to know if you can hard code your column widths. – CryptoFool Oct 05 '20 at 15:19
  • 2
    And `"%-20s"` to align on the left, while `"%20s"` aligns on the right. –  Oct 05 '20 at 15:22
  • Does this answer your question? [Java printf formatting to print items in a table or columns](https://stackoverflow.com/questions/33466526/java-printf-formatting-to-print-items-in-a-table-or-columns) – Nowhere Man Oct 05 '20 at 15:37

1 Answers1

2

Instead of %s%s%s%s, use sufficient space for individual parts e.g. in the following statement, %6s%20s%20s%20s reserves a 6 characters-wide space for students[i].getStudID(), 20 characters-wide space for students[i].getFname(), 20 characters-wide space for students[i].getLname(), and 10 characters-wide space for students[i].getStatus().

System.out.printf("%6s%20s%20s%10s", students[i].getStudID() + " - ", students[i].getFname() + " ", students[i].getLname() + " - ", students[i].getStatus() + " ");

You can adjust the required space as per your requirement.

Learn more about it from the documentation of Formatter. You can also check this for some more examples and explanation.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110