This displays the table to CLI. - Java java.lang

Java examples for java.lang:System

Description

This displays the table to CLI.

Demo Code


import java.util.List;

public class Main{
    /**/*from  www  . java 2s  .  c  o  m*/
     * This displays the table to CLI.
     * @param headers
     * @param commaRecords
     */
    public static void displayTable(List<String> headers,
            List<String> commaRecords) {
        System.out.println(line("+", 80));
        System.out.print("|  ");
        for (String header : headers) {
            System.out.print("\t");
            System.out.print(header);
        }
        System.out.print("\t|");
        System.out.println();
        System.out.println(line("+", 80));
        for (String record : commaRecords) {
            int comma_count = record.length()
                    - record.replace(",", "").length();
            //System.out.println(record);
            String[] r = new String[comma_count + 1];
            String[] ro = record.split(",");
            for (int i = 0; i < ro.length; i++) {
                r[i] = ro[i];
            }
            //System.out.println(r.length);
            System.out.print("|  ");
            for (String s : r) {
                //System.out.print(s+" "+s.length());   
                if (s == null) {
                    System.out.print("");
                } else if (s.equals(CommonUtils.NULL_STRING)) {
                    System.out.print("NULL");
                } else if (s.length() == 0) {
                    System.out.print("NULL");
                } else if (s.equals(CommonUtils.BLANK)) {
                    System.out.print("   ");
                } else {
                    System.out.print(s);
                    //System.out.print(s+" "+s.length());   
                }
                System.out.print("  |  ");
            }
            System.out.println();
        }
        System.out.println(line("+", 80));
    }
    /**
     * @param s The String to be repeated
     * @param num The number of time to repeat String s.
     * @return String A String object, which is the String s appended to itself num times.
     */
    public static String line(String s, int num) {
        String a = "";
        for (int i = 0; i < num; i++) {
            a += s;
        }
        return a;
    }
}

Related Tutorials