Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print an ASCII table?

Tags:

java

ascii

I'm trying to print an ASCII table with each number and its corresponding value, but I don't know how to get the character associated with the number.

public static void main(String[] args) {

    for(char letter= ' ';letter<274;letter++)
       System.out.print(letter);
}

How can I print the associated character with the number?

like image 581
user3571012 Avatar asked Dec 05 '22 06:12

user3571012


2 Answers

Here is a quick implementation you can copy & paste:

import java.util.ArrayList;
import java.util.List;

public class AsciiTable {

    private final List<Column> columns = new ArrayList<>();
    private final List<Row> data = new ArrayList<>();
    private int maxColumnWidth = Integer.MAX_VALUE;

    public void calculateColumnWidth() {

        for (Column column : columns) {
            column.width = column.name.length() + 1;
        }

        for (Row row : data) {
            int colIdx = 0;
            for (String value : row.values) {
                Column column = columns.get(colIdx);
                if (value == null) continue;

                column.width = Math.max(column.width, value.length() + 1);
                colIdx++;
            }
        }

        for (Column column : columns) {
            column.width = Math.min(column.width, maxColumnWidth);
        }
    }

    public void render() {
        StringBuilder sb = new StringBuilder();

        writeSeparator(columns, sb);
        writeColumnNames(columns, sb);
        writeSeparator(columns, sb);

        // values
        writeValues(columns, data, sb);

        writeSeparator(columns, sb);

        System.out.println(sb.toString());
    }

    private void writeColumnNames(final List<Column> columns, final StringBuilder sb) {
        sb.append("|");
        for (Column column : columns) {
            sb.append(String.format(" %-" + (column.width) + "s", column.name));
            sb.append("|");
        }
        sb.append("\n");
    }

    private void writeSeparator(final List<Column> columns, final StringBuilder sb) {
        sb.append("+");
        for (Column column : columns) {
            sb.append(String.format("%-" + (column.width + 1) + "s", "").replace(' ', '-'));
            sb.append("+");
        }
        sb.append("\n");
    }

    private void writeValues(final List<Column> columns, final List<Row> rows, final StringBuilder sb) {
        for (Row row : rows) {
            int columnIdx = 0;
            sb.append("|");
            for (String value : row.values) {

                if (value != null && value.length() > maxColumnWidth)
                    value = value.substring(0, maxColumnWidth - 1);

                sb.append(String.format(" %-" + columns.get(columnIdx).width + "s", value));
                sb.append("|");

                columnIdx++;
            }
            sb.append("\n");
        }
    }

    public List<Column> getColumns() {
        return columns;
    }

    public List<Row> getData() {
        return data;
    }

    public int getMaxColumnWidth() {
        return maxColumnWidth;
    }

    public void setMaxColumnWidth(final int maxColumnWidth) {
        this.maxColumnWidth = maxColumnWidth;
    }

    public static class Column {

        private String name;
        private int width;

        public Column(final String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Column{" +
                    "name='" + name + '\'' +
                    ", width=" + width +
                    '}';
        }
    }

    public static class Row {

        private final List<String> values = new ArrayList<>();

        public List<String> getValues() {
            return values;
        }

        @Override
        public String toString() {
            return "Row{" +
                    "values=" + values +
                    '}';
        }
    }
}

Sample usage for a single column table:

    AsciiTable table = new AsciiTable();
    table.setMaxColumnWidth(45);

    table.getColumns().add(new AsciiTable.Column("my column"));

    for (String value : myListOfValues) {

        AsciiTable.Row row = new AsciiTable.Row();
        table.getData().add(row);
        row.getValues().add(value);
    }

    table.calculateColumnWidth();
    table.render();

Output:

+-----------+
| my column |
+-----------+
| one       |
| two       |
| three     |
+-----------+
like image 86
Michael Böckling Avatar answered Mar 27 '23 21:03

Michael Böckling


It's simple ...

   for (int c=32; c<128; c++) {
    System.out.println(c + ": " + (char)c);
   } 
like image 42
Matej Špilár Avatar answered Mar 27 '23 19:03

Matej Špilár