How to Create Table Using Ascii in a Console

How can I create table using ASCII in a console?

You can use System.out.format() or System.out.printf() (printf internally simply invokes format so both methods give same results).

Below you will find example which will align text to left and fill unused places with spaces. Aligning String to left can be achieved with %-15s, which means:

  • % reserve (placeholder)
  • 15 "places" for characters
  • s of String data-type
  • - and start printing them from left.

If you want to handle digits use d suffix like %-4d for max 4 digit numbers that should be placed at left side of column.

BTW printf doesn't add automatically line separators after printed data, so if we want to move cursor to next line we need to do it ourselves. We can use \r or \n, or let Formatter generate OS dependent line separator (like for Windows \r\n) with %n (note: this "placeholder" doesn't require any data as arguments, Java will provide correct sequence based on OS).

You can find more info about supported syntax at documentation of Formatter class.

String leftAlignFormat = "| %-15s | %-4d |%n";

System.out.format("+-----------------+------+%n");
System.out.format("| Column name | ID |%n");
System.out.format("+-----------------+------+%n");
for (int i = 0; i < 5; i++) {
System.out.format(leftAlignFormat, "some data" + i, i * i);
}
System.out.format("+-----------------+------+%n");

output

+-----------------+------+
| Column name | ID |
+-----------------+------+
| some data0 | 0 |
| some data1 | 1 |
| some data2 | 4 |
| some data3 | 9 |
| some data4 | 16 |
+-----------------+------+

How to create a table with java-ascii-table to display testObject's field values

Essentially, you problem boils down to this line...

asciiTableAware = new CollectionASCIITableAware<TestObject>(arrayListMaker.getTestObjects(), Arrays.asList("ID", "name", "category", "price"), ASCIITable.getInstance().printTable(asciiTableAware);

There is no constructor for List<?>, List<String>, void (and you left off a tailing )... You've merged to lines of code by accident

It should be something more like...

asciiTableAware = new CollectionASCIITableAware<TestObject>(arrayListMaker.getTestObjects(), Arrays.asList("ID", "name", "category", "price"));
ASCIITable.getInstance().printTable(asciiTableAware);

But wait, there's no constructor for List<?>, List<String> either?!? It needs one last parameter, a List<String> which represents the titles...

asciiTableAware = new CollectionASCIITableAware<TestObject>(testObjectsList, Arrays.asList("id", "name", "category", "price"), Arrays.asList("A ID", "First Name", "The Category", "Payup"));
ASCIITable.getInstance().printTable(asciiTableAware);

Ah, now it compiles...

But wait, when we run it...

+------+-------+------------+-------+
| ID | NAME | CATEGORY | PRICE |
+------+-------+------------+-------+
| null | One | This | 10 |
| null | Two | That | 20 |
| null | Three | Other | 30 |
| null | four | something | 40 |
| null | five | else | 50 |
| null | six | over-there | 60 |
| null | seven | Who | 70 |
| null | eight | Why | 80 |
+------+-------+------------+-------+

+------+------------+--------------+-------+
| A ID | FIRST NAME | THE CATEGORY | PAYUP |
+------+------------+--------------+-------+
| null | One | This | 10 |
| null | Two | That | 20 |
| null | Three | Other | 30 |
| null | four | something | 40 |
| null | five | else | 50 |
| null | six | over-there | 60 |
| null | seven | Who | 70 |
| null | eight | Why | 80 |
+------+------------+--------------+-------+

Why are we getting null for ID??!?

The API follows the Java Bean/coding conventions for method names, Code Conventions for the Java TM Programming Language and JavaBeans, this means that it's actually expecting ID to be Id

So if we change the set and get methods of TestObject to be setId and getId and run it again, we get

+----+-------+------------+-------+
| ID | NAME | CATEGORY | PRICE |
+----+-------+------------+-------+
| 11 | One | This | 10 |
| 12 | Two | That | 20 |
| 13 | Three | Other | 30 |
| 14 | four | something | 40 |
| 15 | five | else | 50 |
| 16 | six | over-there | 60 |
| 17 | seven | Who | 70 |
| 18 | eight | Why | 80 |
+----+-------+------------+-------+

+------+------------+--------------+-------+
| A ID | FIRST NAME | THE CATEGORY | PAYUP |
+------+------------+--------------+-------+
| 11 | One | This | 10 |
| 12 | Two | That | 20 |
| 13 | Three | Other | 30 |
| 14 | four | something | 40 |
| 15 | five | else | 50 |
| 16 | six | over-there | 60 |
| 17 | seven | Who | 70 |
| 18 | eight | Why | 80 |
+------+------------+--------------+-------+

How To: Best way to draw table in console app (C#)

You could do something like the following:

static int tableWidth = 73;

static void Main(string[] args)
{
Console.Clear();
PrintLine();
PrintRow("Column 1", "Column 2", "Column 3", "Column 4");
PrintLine();
PrintRow("", "", "", "");
PrintRow("", "", "", "");
PrintLine();
Console.ReadLine();
}

static void PrintLine()
{
Console.WriteLine(new string('-', tableWidth));
}

static void PrintRow(params string[] columns)
{
int width = (tableWidth - columns.Length) / columns.Length;
string row = "|";

foreach (string column in columns)
{
row += AlignCentre(column, width) + "|";
}

Console.WriteLine(row);
}

static string AlignCentre(string text, int width)
{
text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

if (string.IsNullOrEmpty(text))
{
return new string(' ', width);
}
else
{
return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
}
}

How can I pretty-print ASCII tables with Python?

For some reason when I included 'docutils' in my google searches I stumbled across texttable, which seems to be what I'm looking for.

Print ASCII line art characters in C# console application

A small program that modifies the codepage used by the Console.OutputEncoding property to use the characters you desire:

class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.GetEncoding(1252);
Console.WriteLine((char) 169);
Console.WriteLine((char) 170);

for(char c = (char)179; c <= (char)218; ++c)
{
Console.WriteLine(c);
}
}
}

EDIT:

So I went ahead and looked up the Unicode equivalents of the box art. There's a few extra glyphs that may be useful to you. That Wikipedia page lists all of their code points.

I've put together this to try them out:

class Program
{
static void Main(string[] args)
{
for(int i = 0x2500; i <= 0x2570; i += 0x10)
{
for(int c = 0; c <= 0xF; ++c)
{
Console.Write((char) (i + c));
}

Console.WriteLine();
}
}
}

For me, quite a few glyphs simply come up as ?, but the standard box-art glyphs we're used to seeing in the old ASCII games do appear for me. Hopefully these will work for you.



Related Topics



Leave a reply



Submit