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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:21:44+00:00 2026-05-27T22:21:44+00:00

I am writing a program and am stuck in the following requirement: The output

  • 0

I am writing a program and am stuck in the following requirement:

The output goes to a fixed format file

     Total 1                   Total 2
 ----------------          ----------------
       311,862.33                 23,456.00
        40,000.00                789,123.00  

As in above example, the length of the amount string varies.

Requirement is that the amount should always start at the end of the ‘hyphens’.

How to achieve this ?

I know that right and left pad to the string could be acheived through:

String.format("%1$#20s", "311,862.33");
String.format("%1$-15s", "12345");

But how to achieve in my case where there are multiple strings in every line and the string lengths are dynamic ?

Found below code which formats amount in a single column. But how to achieve the same for multiple columns i.e. multiple amounts in same line ?

String format = "%10.2f\n"; // width == 10 and 2 digits after the dot
float [] floats = {123.45f, 99.0f, 23.2f, 45.0f};
for(int i=0; i<floats.length; i++) {
    float value = floats[i];
    System.out.format(format, value);
}

This will give output as:

123.45
 99.00
 23.20
 45.00

Thanks for reading!

  • 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-27T22:21:45+00:00Added an answer on May 27, 2026 at 10:21 pm

    It could be easily done using string format method:

    String amount1 = "311,862.33";
    String amount2 = "45,678.22";
    String content = String.format("%26s", amount1) + String.format("%20s", amount2);
    

    This will give output exactly how I want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing host code for a CUDA program, so I am stuck using
I'm writing a program that's parsing an XML file to java objects using smooks.
I'm writing a program that reads huge file (3x280 GB) and does a fitting
JAVA- Hi, I am writing a minesweeper program (first biggie) and am really stuck.
I'm writing a Java program that queries a PostgreSQL database. I'm following this example
I'm writing a kind of Remote Desktop program and I got stuck with a
For a program that I'm writing, it is useful for me to calculate file
I'm stuck writing my program Here's what I wanted it to do: display a
I am writing the code for the following program, Objective: Count the number of
I am writing a simple program to filter out stock quotes from a given

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.