1

I am trying to align my output like this:

Full Name                Total Sales 
=========                ===========         
John Smith                    619.50 
Mary Willow                   514.50 
Sallie Smite                  519.50 
Tom Andrews                    55.00 
Norman Bates                  366.00 
Horace Williams               301.00 
Anne Whitney                  426.37 
Wallie Jawie                  647.00 
Marie Bunker                   63.00 
Mopsie Bear                  1582.00 
Stephen Andrews               265.00 
Stacie Andrea                 265.00 
Last Name                     463.00 

I am using this code but the alignment is always off

// For loop
    System.out.printf("%s %s %.2f %n " , list[i].getFirstName() , list[i].getLastName(), list[i].getTotalSales() )

This is the result that I get. How can I get the total sales to be aligned? Also the second output has a space in front of the first name, How can I get rid of that?

Full Name                Total Sales         
=========                ===========         
John Smith 619.50 
 Mary Willow 514.50 
 Sallie Smite 519.50 
 Tom Andrews 55.00 
 Norman Bates 366.00 
 Horace Williams 301.00 
 Anne Whitney 426.37 
 Wallie Jawie 647.00 
 Marie Bunker 63.00 
 Mopsie Bear 1582.00 
 Stephen Andrews 265.00 
 Stacie Andrea 265.00 
 Last Name 463.00 
1
  • Are you sure that leading space does not come from your list? In either case you can trim it. Commented Nov 13, 2013 at 23:12

3 Answers 3

2

According to the format syntax, the width should be specified.

System.out.printf("%-20s %10.2f", list[i].getFirstName()  + " " + list[i].getLastName(), list[i].getTotalSales() );
Sign up to request clarification or add additional context in comments.

1 Comment

OP prints FirstName and LastName separately. S/he will have to trim and concatenate to fit your format.
2
  1. Iterate over your collection in order to find out the length of the longest name.
  2. Iterate once again, but this time use %XXs placeholder, where XX is the length. Names will be displayed with trailing spaces up to XX characters.

Also the second output has a space in front of the first name, How can I get rid of that?

You've placed an extra space into your format parameter "...%n ", it should be removed.


public static class Person {
    public String name;
    public double totalSales;

    public Person(String name, double totalSales) {
        this.name = name;
        this.totalSales = totalSales;
    }
}

public static void main (String[] args) throws java.lang.Exception
{
    List<Person> people = new ArrayList<>();
    people.add(new Person("John Smith", 619.50));
    people.add(new Person("Sallie Smite", 519.50));
    people.add(new Person("Horace Williams", 301.00));
    people.add(new Person("Stacie Andrea", 63.00));

    int longestNameLength = 0;

    for (Person person : people) {
        int nameLength = person.name.length();

        if (nameLength > longestNameLength) {
            longestNameLength = nameLength;
        }
    }

    String nameFormat = "%-" + longestNameLength + "s";

    System.out.format(nameFormat + " %s%n", "Name", "Total Sales");

    for (Person person : people) {
        System.out.format(nameFormat + " %.2f%n", person.name, person.totalSales);
    }
}

Outputs

Name            Total Sales
John Smith      619.50
Sallie Smite    519.50
Horace Williams 301.00
Stacie Andrea   63.00

Tips:

  1. Increment the value of longestNameLength by ~10 to create a margin.
  2. To align total sales column to the right, you can use following trick:

    System.out.format("%-10s %10s", person.name, String.format("%.2f", person.totalSales));
    

3 Comments

In OP's case Person class has separate fields for first and last names.
It doesn't really matter. Insted of person.getFullName() just type person.getFirstName() + " " + person.getLastName() or create mentioned method inside Person class.
Sure. My comment was for the benefit of OP and other visitors.
0

try this

//for loop
System.out.printf("%s %s %.2f %n " , list[i].getFirstName() , list[i].getLastName()+"\t"+ list[i].getTotalSales() );

where this \t tab on console put this tabs how many you want

1 Comment

When you are already dealing with formatted output adding \ts is just plain ugly. Plus it's difficult to predict how many you would need.

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.