Issue
In a school project, I need to print string expressions one under the other in the same alignment. How can I do that.
For example, in a situation like here, how can I print names, surnames and numbers in the same line, one under the other? Thank you in advance to everyone who answers and helps.
Solution
Your question isn't very clear, but I think you want to print a table of data out in Java. Try using the System.out.format() method. Here's a quick example of how to use it:
System.out.format("%-15s %-15s %-15s\n", "Column 1", "Column 2", "Column 3");
System.out.format("%-15s %-15s %-15s\n", "Hello", "World", "!");
System.out.format("%-15s %-15s %-15s\n", "This", "is", "a table");
This will output the following table to the console:
Column 1 Column 2 Column 3
Hello World !
This is a table
If you want to display the table in a more sophisticated way, (right now, it might not be very clear that it's a table at all!), you can try using a third-party library such as j-text-utils or ASCIITable. These libraries provide more advanced features such as sorting, row numbering, and more. Here's an example of how to use j-text-utils to print a table:
TextTable tt = new TextTable(new String[]{"Column 1", "Column 2", "Column 3"}, new String[][]{
{"Hello", "World", "!"},
{"This", "is", "a table"}
});
tt.printTable();
This will output the following table to the console:
+---------------+---------------+---------------+
| Column 1 | Column 2 | Column 3 |
+---------------+---------------+---------------+
| Hello | World | ! |
+---------------+---------------+---------------+
| This | is | a table |
+---------------+---------------+---------------+
If this isn't what you want to do, please update your question with more details. It's not very clear right now.
Answered By - SE User
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.