java implementation of console table

Posted by vincent30000 on Thu, 17 Oct 2019 22:10:54 +0200

Draw a form

It's easy to make beautiful tables in a graphical environment. But it's more difficult in a console environment. Sometimes you can use some symbols to roughly simulate: (word documents may not be neat, copy to Notepad to see)
+-------+------+
|abc    |xyz=tt|
+-------+------+
|hellomm|t2    |
+-------+------+

This topic requires the design of a program to show the user's input in this "quasi table" way. The specific requirements are:
The first line entered by the user is an integer indicating how many lines of information there are next. Each of the next lines consists of several cells. The cells are separated by commas.
Program output: input content re presented in tabular form.
For example:

User input:
cat,dog,good-luck
1,2,5
do not use,,that
 Then the program output: (the word document may not be neat, copy it to Notepad)
+----------+---+---------+
|cat       |dog|good-luck|
+----------+---+---------+
|1         |2  |5        |
+----------+---+---------+
|do not use|   |that     |
+----------+---+---------+
It is easy to see that:
Two consecutive commas indicate that there is an empty cell in the middle
 The number of columns is determined by the row with the largest number of cells
 The width of a column is determined by the widest cell in the same column
 Left alignment of information in cells

It can be assumed that the maximum number of rows entered by the user is 30 and the maximum number of possible columns is 40.
package com.liu.ex8;

import java.util.Scanner;

public class Main {
    
    public void printResult(String[] A) {
        String[] tempA = A[0].split(",");
        int maxLen = tempA.length;
        for(int i = 1;i < A.length;i++) {
            tempA = A[i].split(",");
            if(maxLen < tempA.length)
                maxLen = tempA.length;
        }
        String[][] valueA = new String[A.length][maxLen];
        for(int i = 0;i < valueA.length;i++)
            for(int j = 0;j < valueA[0].length;j++)
                valueA[i][j] = "";
        
        for(int i = 0;i < A.length;i++) {
            tempA = A[i].split(",");
            for(int j = 0;j < tempA.length;j++)
                valueA[i][j] = tempA[j];
        }
        int[] maxJ = new int[maxLen];
        for(int j = 0;j < maxLen;j++) {
            for(int i = 0;i < A.length;i++) {
                if(valueA[i][j].length() > maxJ[j])
                    maxJ[j] = valueA[i][j].length();
            }
        }
        
        StringBuilder opera = new StringBuilder("+");
        for(int j = 0;j < maxJ.length;j++) {
            for(int k = 0;k < maxJ[j];k++)
                opera.append('-');
            opera.append('+');
        }
        for(int i = 0;i < valueA.length;i++) {
            System.out.println(opera);
            System.out.print("|");
            for(int j = 0;j < valueA[0].length;j++) {
                int len = maxJ[j] - valueA[i][j].length();
                String format = "";
                if(len == 0)
                    format = "" + "%s";
                else 
                    format = "%" + len + "s";
                System.out.print(valueA[i][j]);
                System.out.printf(format, "");
                System.out.print("|");
            }
            System.out.println();
        }
        System.out.println(opera);
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        String[] A = new String[n];
        for(int i = 0;i < n;i++)
            A[i] = in.nextLine();
        test.printResult(A);
    }
}

Topics: Java