### Text formatting: Print in 2 columns format

• Solving Sub-problem 3: print the formatted text out in 2 columns

• Question:

 What would you do if someone ask you to "print a series of line of text in 2 columns such that the height of the columns differ in at most 1 line ???

To get a sense of the problem, we again use a concrete example:

• Here is the output of the previous step (a serie of formatted lines)

The lines are stored in an array of String

 ``` Array index [0] WE hold these Truths to be [1] self-evident, that all Men are [2] created equal, that they are [3] endowed by their Creator with [4] certain unalienable Rights, [5] that among these are Life, [6] Liberty and the Pursuit of [7] Happiness. ```

• We must printed it like this:

 ``` WE hold these Truths to be certain unalienable Rights, self-evident, that all Men are that among these are Life, created equal, that they are Liberty and the Pursuit of endowed by their Creator with Happiness. ```

• Solution:

• Print the first line using these output lines:

 ``` WE hold these Truths to be <------- line 1 self-evident, that all Men are created equal, that they are endowed by their Creator with certain unalienable Rights, <------- line 1 that among these are Life, Liberty and the Pursuit of Happiness. ```

• Print the second line using these output lines:

 ``` WE hold these Truths to be self-evident, that all Men are <---- Line 2 created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, <---- Line 2 Liberty and the Pursuit of Happiness. ```

• And so on....

• 2 cases: even and odd number of lines

• We need to distinguish 2 cases:

• Text contains an even number of lines.

Example:

 ``` Array index [0] WE hold these Truths to be [1] self-evident, that all Men are [2] created equal, that they are [3] endowed by their Creator with [4] certain unalienable Rights, 8/2 = 4 [5] that among these are Life, [6] Liberty and the Pursuit of [7] Happiness. ```

Output:

 ``` [0] WE hold these Truths to be [4] certain unalienable Rights, [1] self-evident, that all Men are [5] that among these are Life, [2] created equal, that they are [6] Liberty and the Pursuit of [3] endowed by their Creator with [7] Happiness. ```

• Text contains an odd number of lines.

Example:

 ``` [0] WE hold these Truths to be [1] self-evident, that all Men are [2] created equal, that they are [3] endowed by their Creator with [4] certain unalienable Rights, [5] that among these are Life, 5 = 9/2 + 1 [6] Liberty and the Pursuit of [7] Happiness. xxxx xxxx xxxx xxxx [8] xxxx xxxxx. ```

Output:

 ``` [0] WE hold these Truths to be [5] that among these are Life, [1] self-evident, that all Men are [6] Liberty and the Pursuit of [2] created equal, that they are [7] Happiness. xxxx xxxx xxxx xxxx [3] endowed by their Creator with [8] xxxx xxxxx. [4] certain unalienable Rights, ```

• Algorithm to print lines (stored in an array of String) in 2 columns:

 ``` half = (number of lines) / 2; if ( number of lines is even ) { line 1: a[0] + a[ half ] line 2: a[1] + a[ half + 1] line 3: a[2] + a[ half + 2] ... last: a[half-1] + a[ half + (half - 1)] } else { line 1: a[0] + a[ half + 1] line 2: a[1] + a[ half + 2] line 3: a[2] + a[ half + 3] ... last-1: a[half-1] + a[ half + half ] last: a[half] } ```

• Writing it like this, makes the for-loop more explicit:

 ``` half = (number of lines) / 2; if ( number of lines is even ) { line 1: a[0] + a[ half + 0] line 2: a[1] + a[ half + 1] line 3: a[2] + a[ half + 2] ... last: a[(half-1)] + a[ half + (half - 1)] } else { line 1: a[0] + a[ half + 1 + 0] line 2: a[1] + a[ half + 1 + 1] line 3: a[2] + a[ half + 1 + 2] ... last-1: a[half-1] + a[ half + 1 + (half-1) ] last: a[half] } ```

• Java method:

 ``` /* ================================================== Input: t = array of text nLines = number of lines in array t =================================================== */ public static void printText( String[] t, int nLines ) { int i, half; half = nLines/2; if ( nLines % 2 == 0 ) { // **************************************** // Both columns have same number of lines // **************************************** for (i = 0; i < half; i++) System.out.println(t[i] + " " + t[i+half]); } else { // **************************************** // First column has one more line // **************************************** for (i = 0; i < half; i++) System.out.println(t[i] + " " + t[i+half+1]); System.out.println(t[half]); // Extra line for column 1 } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac FormatText.java To run:          java FormatText