Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6621711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:18:12+00:00 2026-05-25T21:18:12+00:00

I need to display a table in a console. My simple solution, if you

  • 0

I need to display a table in a console.

My simple solution, if you would call it a “solution”, is as follows:

  override def toString() = {
    var res = "\n"
      var counter = 1;
      res += stateDb._1 + "\n"
      res += "  +----------------------------+\n"
      res += "  +     State Table            +\n"
      res += "  +----------------------------+\n"
      for (entry <- stateDb._2) {
        res += "  | " + counter + "\t | " + entry._1 + " | " + entry._2 + " |\n"
        counter += 1;
      }
      res += "  +----------------------------+\n"
      res += "\n"
    res

  }

We don’t have to argue this

  • a is looking bad when displayed
  • b code looks kinda messed up

Actually, such a question was asked for C# but I would like to know a nice solution for Scala as well.

So what is a (nice/good/simple/whatever) way to draw such a table in Scala to the console?

-------------------------------------------------------------------------
|    Column 1     |    Column 2     |    Column 3     |    Column 4     |
-------------------------------------------------------------------------
|                 |                 |                 |                 |
|                 |                 |                 |                 |
|                 |                 |                 |                 |
-------------------------------------------------------------------------
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:18:13+00:00Added an answer on May 25, 2026 at 9:18 pm

    I’ve pulled the following from my current project:

    object Tabulator {
      def format(table: Seq[Seq[Any]]) = table match {
        case Seq() => ""
        case _ => 
          val sizes = for (row <- table) yield (for (cell <- row) yield if (cell == null) 0 else cell.toString.length)
          val colSizes = for (col <- sizes.transpose) yield col.max
          val rows = for (row <- table) yield formatRow(row, colSizes)
          formatRows(rowSeparator(colSizes), rows)
      }
    
      def formatRows(rowSeparator: String, rows: Seq[String]): String = (
        rowSeparator :: 
        rows.head :: 
        rowSeparator :: 
        rows.tail.toList ::: 
        rowSeparator :: 
        List()).mkString("\n")
    
      def formatRow(row: Seq[Any], colSizes: Seq[Int]) = {
        val cells = (for ((item, size) <- row.zip(colSizes)) yield if (size == 0) "" else ("%" + size + "s").format(item))
        cells.mkString("|", "|", "|")
      }
    
      def rowSeparator(colSizes: Seq[Int]) = colSizes map { "-" * _ } mkString("+", "+", "+")
    }
    
    scala> Tabulator.format(List(List("head1", "head2", "head3"), List("one", "two", "three"), List("four", "five", "six")))
    res1: java.lang.String = 
    +-----+-----+-----+
    |head1|head2|head3|
    +-----+-----+-----+
    |  one|  two|three|
    | four| five|  six|
    +-----+-----+-----+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a RSS feed i need to display in a table (its clothes
I need to display a list of dates, which I have in a table
I have a large database table that I need to display on a Windows
I need to display a table with various info (names, dates, serial numbers, notes).
I need to display a table in rich text within a form window. It
Here is my problem. I need to display an html table with 9 cells
I have some hierarchical data that I need display as a table, can I
I need to display what the table contains from the freshest data to the
I need to display some details on DetailsView from two tables. I have table:
i am using display tag for showing table in my jsp page. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.