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

 

 

First, let's take a look at what we get if we just use System.out.print[ln] calls.

For example, let's produce a table from some employee data.  

Our first attempt will be pretty crude:

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

/**
 * Class to represent an employee
 */
class Employee1
{
  Employee1(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.print("|");
    System.out.print("ID" + "|");
    System.out.print("Name" + "|");
    System.out.print("Address" + "|");
    System.out.print("Date of Birth" + "|");
    System.out.println("Salary" + "|");  }
  
  void print()
  {
    System.out.print("|");
    System.out.print(m_id + "|");
    System.out.print(m_name + "|");
    System.out.print(m_address + "|");
    System.out.print(m_dob + "|");
    System.out.println(m_salary + "|");
  }
  
  private int     m_id;
  private String  m_name;
  private String  m_address;
  private Date    m_dob;
  private double  m_salary;
}
Notes:
  • I have used the Date class.  The particular constructor for Date that I used is officially deprecated.  However, I am using it because it is the simplest form to use for this example (Calendar, and GregorianCalendar are far to complex!)
  • Other annoyances with the Date class include:
    • The year must be specified from a base of 1900
    • The month number is zero-based

When run, this program produces the following output:

|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|
Not exactly the best format one could hope for, right?

 

The page was last updated February 19, 2008