6

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?

1
  • 1
    You should probably skip over 127-159, e.g. if (letter < 127 || letter > 159) { System.out.print(...) }. The characters in the range 127-159 are control characters, not printable characters, and they may not display anything useful and could have some interesting effects. Many Windows systems assign some printable text to those positions, though, so it may work. Don't count on it. Commented Apr 24, 2014 at 23:56

3 Answers 3

10

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     |
+-----------+
Sign up to request clarification or add additional context in comments.

Comments

9

It's simple ...

   for (int c=32; c<128; c++) {
    System.out.println(c + ": " + (char)c);
   } 

1 Comment

Umm, that could do some interesting things to the output. Please start at c=32.
0

Have a look at java.util.Formatter:

    for(char letter= ' ';letter<274;letter++) {
        System.out.printf("%d = %c\n", (int)letter, letter);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.