Print a table in cmd in java
I have the following matrix
1 2 3 4 5 6
2 3 4 5 6 1
3 4 5 6 1 2
4 5 6 1 2 3
5 6 1 2 3 4
6 1 2 3 4 5
...and I want to have it in a table
| 1 2 3 4 5 6
---------------
1| 1 2 3 4 5 6
2| 2 3 4 5 6 1
3| 3 4 5 6 1 2
4| 4 5 6 1 2 3
5| 5 6 1 2 3 4
6| 6 1 2 3 4 5
Having n elements is it possible to print such a table dynamically?
public static void main ( String [ ] args ) {
int n = Integer.parseInt(args[0]);
for(int w = 1; w <= n; w++){
System.out.println("");
for(int p = w; p <= n + w -开发者_运维问答 1; p++){
System.out.print((p-1)%n+1 + " ");
}
}
}
Depends on the array, assuming that you know the array size beforehand sure. In Java arrays can be jigged aka subarrays do not have to be same size, what in mind:
For test case:
public static void main(String[] args) {
System.out.println(toTableString(new int[][] { { 1, 2, 3, 4, 5, 6 },
{ 2, 3, 4, 5, 6, 1 }, { 3, 4, 5, 6, 1, 2 },
{ 4, 5, 6, 1, 2, 3 }, { 5, 6, 1, 2, 3, 4 },
{ 6, 1, 2, 3, 4, 5 } }));
System.out.println();
System.out.println(toTableString(new int[][] { { 1 }, { 2, 3 },
{ 3, 4, 5 }, { 4, 5, 6, 1 }, { 5, 6, 1, 2, 3 },
{ 6, 1, 2, 3, 4, 5 } }));
System.out.println();
System.out.println(toTableString(new int[][] { { 1 }, { 20, 300 },
{ 3000, 40000, 50000 }}));
}
Output:
| 0 1 2 3 4 5
——————————————
0| 1 2 3 4 5 6
1| 2 3 4 5 6 1
2| 3 4 5 6 1 2
3| 4 5 6 1 2 3
4| 5 6 1 2 3 4
5| 6 1 2 3 4 5
| 0 1 2 3 4 5
——————————————
0| 1
1| 2 3
2| 3 4 5
3| 4 5 6 1
4| 5 6 1 2 3
5| 6 1 2 3 4 5
| 00000 00001 00002
————————————————————————
00000| 00001
00001| 00020 00300
00002| 03000 40000 50000
Run online: link
This can take pretty much anything you can throw at it:
public static void main(String[] args) {
int[][] matrix = {
{ 1, 2, 3, 4, 5, 6 },
{ 2, 3, 4, 5, 6, 1 },
{ 3, 4, 5, 6, 1, 2 },
{ 4, 5, 6, 11, 2, 3 },
{ 5, 116, 1, 2, 3, 4 },
{ 5, 116, 1, 2, 3, 4 },
{ 5, 116, 1, 2, 3, 4 },
{ 5, 116, 1, 2, 3, 4 },
{ 5, 223523526, 1, 2, 3, 4 },
{ 5, 116, 1, 2, 3, 4 },
{ 5, 116, 1, 2, 3, 4 },
{ 5, 116, 1, 2, 3, 4 },
{ 5, 116, 1, 2, 3, 4 },
{ 6, 1, 2, 3, 4, 5 }
};
//compute the maximum header width (the number of columns)
int rows = matrix.length;
int cols = rows > 0 ? matrix[0].length : 0;
int maxRowHeaderWidth = Integer.toString(rows).length();
int maxCellWidth = 1;
//compute the maximum cell width
for ( int[] row : matrix ) {
for ( int cell : row ) {
maxCellWidth = Math.max(Integer.toString(cell).length(), maxCellWidth);
}
}
maxCellWidth = Math.max(maxCellWidth, Integer.toString(cols).length());
StringBuilder patternBuilder = new StringBuilder();
//the header
for ( int i = 0; i < maxRowHeaderWidth; i++ ) {
patternBuilder.append(' ');
}
patternBuilder.append('|');
for ( int i = 0; i < cols; i++ ) {
patternBuilder.append(String.format("%" + (maxCellWidth+1) + "d", i+1));
}
patternBuilder.append('\n');
//the line of dashes
int rowWidth = patternBuilder.length();
for ( int i = 0; i < rowWidth; i++ ) {
patternBuilder.append('-');
}
patternBuilder.append('\n');
//each row
for ( int i = 0; i < matrix.length; i++ ) {
patternBuilder.append(String.format("%" + (maxRowHeaderWidth) + "d|", i+1));
for ( Integer cell : matrix[i] ) {
patternBuilder.append(String.format("%" + (maxCellWidth+1) + "d", cell));
}
patternBuilder.append('\n');
}
System.out.println(patternBuilder);
}
It sizes every cell equally, regardless of the length of the number in the cell. It can take an arbitrary number of rows or columns.
Crude, But should work.
//This method adds required number spaces for alignment.
private static String formatString(String s, int in) {
return String.format("%1$-" + (in + 1) + "s", s);
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int in = args[0].length();
System.out.print(formatString(" ", in));
System.out.print("| ");
for (int w = 1; w <= n; w++) {
System.out.print(formatString(w + "", in));
}
System.out.println("");
for (int w = 1; w <= n + 2; w++) {
System.out.print(formatString("-", in));
}
for (int w = 1; w <= n; w++) {
System.out.println("");
System.out.print(formatString((w + ""), in));
System.out.print("| ");
for (int p = w; p <= n + w - 1; p++) {
System.out.print(formatString(((p - 1) % n + 1) + "", in));
}
}
}
EDIT: Edited the code to consider alignment issues.
If I were you, I would take a look at libraries like OpenCSV. Although not specially fitted to your need, they will be of great use.
Indeed, your table display can be seen as a way to print a CSV file. As a consequence, building the structure fitted to this CSV file and printing it to System.out could do the job with a clean layout (although absolutely not optimal in terms of dependencies).
Output:
+------------+--------------+------------+
| one | two | three |
+------------+--------------+------------+
| super | broccoli | flexible |
| assumption | announcement | reflection |
| logic | pleasant | wild |
+------------+--------------+------------+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CommandLineTable {
private static final String HORIZONTAL_SEP = "-";
private String verticalSep;
private String joinSep;
private String[] headers;
private List<String[]> rows = new ArrayList<>();
private boolean rightAlign;
public CommandLineTable() {
setShowVerticalLines(false);
}
public void setRightAlign(boolean rightAlign) {
this.rightAlign = rightAlign;
}
public void setShowVerticalLines(boolean showVerticalLines) {
verticalSep = showVerticalLines ? "|" : "";
joinSep = showVerticalLines ? "+" : " ";
}
public void setHeaders(String... headers) {
this.headers = headers;
}
public void addRow(String... cells) {
rows.add(cells);
}
public void print() {
int[] maxWidths = headers != null ?
Arrays.stream(headers).mapToInt(String::length).toArray() : null;
for (String[] cells : rows) {
if (maxWidths == null) {
maxWidths = new int[cells.length];
}
if (cells.length != maxWidths.length) {
throw new IllegalArgumentException("Number of row-cells and headers should be consistent");
}
for (int i = 0; i < cells.length; i++) {
maxWidths[i] = Math.max(maxWidths[i], cells[i].length());
}
}
if (headers != null) {
printLine(maxWidths);
printRow(headers, maxWidths);
printLine(maxWidths);
}
for (String[] cells : rows) {
printRow(cells, maxWidths);
}
if (headers != null) {
printLine(maxWidths);
}
}
private void printLine(int[] columnWidths) {
for (int i = 0; i < columnWidths.length; i++) {
String line = String.join("", Collections.nCopies(columnWidths[i] +
verticalSep.length() + 1, HORIZONTAL_SEP));
System.out.print(joinSep + line + (i == columnWidths.length - 1 ? joinSep : ""));
}
System.out.println();
}
private void printRow(String[] cells, int[] maxWidths) {
for (int i = 0; i < cells.length; i++) {
String s = cells[i];
String verStrTemp = i == cells.length - 1 ? verticalSep : "";
if (rightAlign) {
System.out.printf("%s %" + maxWidths[i] + "s %s", verticalSep, s, verStrTemp);
} else {
System.out.printf("%s %-" + maxWidths[i] + "s %s", verticalSep, s, verStrTemp);
}
}
System.out.println();
}
public static void main(String[] args) {
//test code
CommandLineTable st = new CommandLineTable();
//st.setRightAlign(true);//if true then cell text is right aligned
st.setShowVerticalLines(true);//if false (default) then no vertical lines are shown
st.setHeaders("one", "two", "three");//optional - if not used then there will be no header and horizontal lines
st.addRow("super", "broccoli", "flexible");
st.addRow("assumption", "announcement", "reflection");
st.addRow("logic", "pleasant", "wild");
st.print();
}
}
精彩评论