import java.io.*;
/**
* Writes formatted tables (i.e., 2-dimensional arrays of
* String objects) to a PrintWriter
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class TableWriter
{
	private int fieldWidth;
	private PrintWriter out;

 /**
  * Explicit Value Constructor
  *
  * @param out The PrintWriter to write to
  */
  public TableWriter(PrintWriter out)
  {
		this.out = out;
		fieldWidth = 12;
  }



 /**
  * Print a table
  *
  * @param table The table to print
  */
  public void printTable(String[][] table)
  {
		FieldFormat formatter;
		int i, j;
		formatter = new FieldFormat();
		formatter.setFieldWidth(fieldWidth);

		for (i=0; i < table.length; i++)
		{
		   System.out.println ("table.length is " + table.length);
			for (j=0; j <table[i].length; j++)
			{
			   System.out.println (" table[" + i+ "].length is " + table[i].length);
				if (table[i][j] == null)
				{
					out.print
						(formatter.fitStringToField(" "));
				}  // end if
				else
				{
					out.print
						(formatter.fitStringToField(table[i][j]));
				} // end else
			}// end for j
		out.println();
 	}  // end for i
} // end printTable

 /**
  * Print a table with headers
  *
  * @param table The table to print
  */
  public void printTableWithHeaders(String[][] table)
  {
		FieldFormat formatter;
		int i, j;
		formatter = new FieldFormat();
		formatter.setFieldWidth(fieldWidth);
      
		// print header row (column indicators)
		out.print ("");
		for (i = 0; i < table.length; i++)
		{  
		   // System.out.print ("     " + i + "    ");
			 out.print(formatter.fitStringToField(i + " "));
		}
		// System.out.println ();
		   out.println();
  
		for (i=0; i < table.length; i++)
	   {
		   out.print(i); //row indicators
		  // System.out.println ("table.length is " + table.length);
			for (j=0; j <table[i].length; j++)
			{
			  // System.out.println (" table[" + i+ "].length is " + table[i].length);
				if (table[i][j] == null)
				{
					out.print
						(formatter.fitStringToField(" "));
				}  // end if
				else
				{
					out.print
						(formatter.fitStringToField(table[i][j]));
				} // end else
			}// end for j
		out.println();
 	}  // end for i
} // end printTable with headers

/**
*  Prints each element in the table preceeded by its element reference
*  (i.e., row,column) and a ":" followed by a \t
*
*   @param table
*/

public void writeTable(String[][] table)
{
      int i,j;
		for (i=0; i < table.length; i++)
		{
		 	for (j=0; j <table[i].length; j++)   
         {
			   System.out.print (i + "," + j +":"+table[i][j]+ ",\t ");
		   } // end for j
			System.out.println();
		} // end for i
}// end writeTable
			
 /**
  * Set the field width to use for each
  * entry in the table
  *
  * @param width
  */
  public void setFieldWidth(int width)
  {
		fieldWidth = width;
  }	// end setFieldWidth


}// end TableWriter