Fixing Some Problems
Home ] Up ] Using Plain print & println ] [ Fixing Some Problems ] Width, Spacing & Justification ]

 

 

So, let's see...  Here's what we have so far:

|ID|Name|Address|Date of Birth|Salary|
|1|Bloggs, Fred|10 Firebird Crescent|Thu May 20 00:00:00 EDT 1976|40000.0|
|2|Charles, Cheryl|5 Lattice Lane|Mon Apr 03 00:00:00 EDT 1989|50000.0|
|3|Romney, Titus|42 St. Martins Rd.|Mon Nov 19 00:00:00 EST 1973|45000.0|
|4|Clinton, Burton|567 Nebula Circle|Wed Jan 29 00:00:00 EST 1964|100000.0|
|5|McDuff, Hazel|1 Church Row|Wed Mar 06 00:00:00 EST 1985|55000.0|
|6|Frazier, Bill|92 S. Main St.|Fri Dec 05 00:00:00 EST 1980|70000.0|
|7|Zambroski, Heather|589 Trumpington Place|Fri Apr 03 00:00:00 EST 1959|50000.0|

What are the problems?  Well, here are some:

  1. The most obvious is that the lines that are intended to separate the data items don't line up.
  2. The default formatting of the date of birth is likely to be gobbledygook to most people.  Besides, we don't want the time of birth in there!
  3. Conventionally, monetary amounts, like salary, are presented with a fixed, two digit, size for cents (the fractional part).  Also, wouldn't we like to know about monetary units?  Is the value in dollars or euros or pounds, or what?  And wouldn't it be nicer to use commas, so we can read it more easily ($40,000.00, for example)?
  4. Perhaps we would expect an ID number to include some number of leading zeros -- say, 0001, instead of simply 1.  It's conventional to make an ID be of fixed size by inserting those leading zeros.

Let's see if we can fix the last three of these, by switching to using some printf features:

package inputOutput;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Class to produce a table of information about customers
 */
public class EmployeeTablePrinter2
{
  public static void main(String[] args)
  {
    List<Employee2> employees = new ArrayList<Employee2>();
    
    // Populate list with Employees
    employees.add( new Employee2(1, "Bloggs, Fred", "10 Firebird Crescent", 
                                new Date(1976 - 1900, 5 - 1, 20), 40000.00 ) );
    employees.add( new Employee2(2, "Charles, Cheryl", "5 Lattice Lane",
                                new Date(1989 - 1900, 4 - 1, 3), 50000.00 ) );
    employees.add( new Employee2(3, "Romney, Titus", "42 St. Martins Rd.",
                                new Date(1973 - 1900, 11 - 1, 19), 45000.00 ) );
    employees.add( new Employee2(4, "Clinton, Burton", "567 Nebula Circle",
                                new Date(1964 - 1900, 1 - 1, 29), 100000.00 ) );
    employees.add( new Employee2(5, "McDuff, Hazel", "1 Church Row",
                                new Date(1985 - 1900, 3 - 1, 6), 55000.00 ) );
    employees.add( new Employee2(6, "Frazier, Bill", "92 S. Main St.",
                                new Date(1980 - 1900, 12 - 1, 5), 70000.00 ) );
    employees.add( new Employee2(7, "Zambroski, Heather", "589 Trumpington Place",
                                new Date(1959 - 1900, 4 - 1, 3), 50000.00 ) );
    
    // Print out the table of employees
    Employee2.printHeader();
    for (Employee2 emp : employees)
    {
      emp.print();
    }
  }
}

/**
 * Class to represent an employee
 */
class Employee2
{
  Employee2(int id, String name, String address, Date dob, double salary)
  {
    m_id = id;
    m_name = name;
    m_address = address;
    m_dob = dob;
    m_salary = salary;
  }
  
  static void printHeader()
  {
    System.out.printf("|ID|Name|Address|Date of Birth|Salary|\n");
  }
  
  void print()
  {
    System.out.printf("|%04d|%s|%s|%tF|$%,.2f|\n", 
                      m_id, m_name, m_address, m_dob, m_salary);
  }
  
  private int     m_id;
  private String  m_name;
  private String  m_address;
  private Date    m_dob;
  private double  m_salary;
}

which now produces the following:

|ID|Name|Address|Date of Birth|Salary|
|0001|Bloggs, Fred|10 Firebird Crescent|1976-05-20|$40,000.00|
|0002|Charles, Cheryl|5 Lattice Lane|1989-04-03|$50,000.00|
|0003|Romney, Titus|42 St. Martins Rd.|1973-11-19|$45,000.00|
|0004|Clinton, Burton|567 Nebula Circle|1964-01-29|$100,000.00|
|0005|McDuff, Hazel|1 Church Row|1985-03-06|$55,000.00|
|0006|Frazier, Bill|92 S. Main St.|1980-12-05|$70,000.00|
|0007|Zambroski, Heather|589 Trumpington Place|1959-04-03|$50,000.00|

Notes:

  • The ID values are all now 4 characters wide, with leading zeros
  • The date format is more readable (I chose ISO date format -- YYYY-MM-DD -- because US date formatting conventions conflict with those used elsewhere in the world.  But you can always change it to your own taste.)
  • The salary now specifies that it is in dollars, and always shows two decimal places and is comma-separated.
 
The page was last updated February 19, 2008