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 8972859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:19:10+00:00 2026-06-15T18:19:10+00:00

Halo, I have been looking for a way to generate a nicely formatted report

  • 0

Halo,

I have been looking for a way to generate a nicely formatted report in text file using java.

For example, I might need to print a report in the following format

A                              Monthly Report
A                               Report Name                                      Page No: 1
A Date: YYYY-MM-DD              
A
A Category                        Quantity                   Price
A -----------------               -----------------          --------------------
B Pen                             100                        $100
B Paper                           200                        $400
A
A =================                                          ====================
B Total                                                      $500
A =================                                          ====================

I have tried writing my own program, but I just feel that its a mess!!! So I am wondering if there are any existing library that I can use or is there a good way to implement it??

By the way, I have look around and found a library that are similar to what I want
https://github.com/iNamik/Java-Text-Table-Formatter

Just wondering if there are other options. Thanks for helping!!

====================================================================

So I have made a sample code that I probably will use to clean up my code

    StringBuilder sb = new StringBuilder();

    sb.append(String.format("%s %50s%n", "A", "Monthly Report"));
    sb.append(String.format("%s %48s%n", "A", "Report Name"));
    sb.append(String.format("%s %n", "A"));
    sb.append(String.format("%s %-20s %-20s %-20s%n", "A", "Category", "Quantity", "Price"));
    sb.append(String.format("%s %-20s %-20s %-20s%n", "A", "--------------", "--------------", "--------------"));
    sb.append(String.format("%s %-20s %-20s %-20s%n", "B", "Paper", 100, "$200"));

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


Output:


A                                     Monthly Report
A                                      Report Name
A 
A Category             Quantity             Price               
A --------------       --------------       --------------      
B Paper                100                  $200                


I am thinking how can I make the “Report Name” at the center and “Page No:” at the right without hard coding the int argument of the formatter (i.e. %50s, without 50, is it possible)

  • 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-06-15T18:19:11+00:00Added an answer on June 15, 2026 at 6:19 pm

    Just as an alternative, here is JDK based solution

    public static void main(String[] args) throws Exception {
        printRow("A", "Category", "Quantity", "Price");
        printRow("A", "--------------", "--------------", "--------------");
        printRow("B", "Paper", 100, 200);
    }
    
    private static void printRow(String c0, String c1, Object c2, Object c3 ) {
        System.out.printf("%s %-20s %-20s %-20s%n", c0, c1, String.valueOf(c2), c3 instanceof Integer ? "$" + c3 : c3);
    }
    

    output

    A Category             Quantity             Price               
    A --------------       --------------       --------------      
    B Paper                100                  $200                
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I know, the support for library://ns.adobe.com/flex/halo namespace has been dropped, and
I am using SLD to style output from my geoserver. I have couple types
I would like to return the ids which have been deleted by a DELETE
Possible Duplicate: every derived table must have its own alias Halo i am trying
I have these following lines to send bytes using socket Dim server As New
I am using Squeak (can't use Pharo), and I have a Morphic application and
Halo, I have this xml document: <document> <Line> <Line-Item> <ID>5</ID> <Quantity>100</Quantity> </Line-Item> </Line> <Line>
I have a few radio buttons that I'm styling with PNG background images, using
Halo I'm sure this question has been asked many times in the past, but
Possible Duplicate: What are the downsides to using Spark over Halo in Flex? I

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.