import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class FormatText { public static final int NCHARS_IN_LINE = 30; public static String[] words = new String[10000]; // Hold words in input file public static int currWord; // Current word public static int numWords; // Count # words in input public static int readInput( Scanner in, String[] w ) { int nWords = 0; while ( in.hasNext() ) { w[nWords] = in.next(); // Read next string (word) nWords++; // Count # words AND use next // array element for next word } return( nWords ); } public static String readOneLine( String[] w, int maxLineLength ) { String line; line = w[ currWord ++ ]; while ( currWord < numWords && line.length() + 1 + w[currWord].length() <= maxLineLength ) { line = line + " " + w[currWord++]; // Add word to line } return(line); } // *********************************************************************** // fixLine(s, maxLineLength): make line s right adjusted to n characters // *********************************************************************** public static String fixLine(String s, int maxLineLength) { int NGaps; int NTrailingSpaces; int NSpacesPerGap; // # spaces to insert in each gap int NExtra; // # gaps that gets ONE extra space String outputLine; // Output string... StringTokenizer T; // Help variables... int i, k; /* ------------------------------------------------- Check if line is already right-adjusted.... ------------------------------------------------- */ if ( s.length() == maxLineLength ) return(s); // Already right-justified /* -------------------------------------- Compute: NTrailingSpaces and NGaps -------------------------------------- */ NTrailingSpaces = maxLineLength - s.length(); // # trailing spaced T = new StringTokenizer(s); NGaps = T.countTokens() - 1; // # gaps /* ------------------------------------------------- Check if line has > 1 word.... (Lines with 1 word can't be right-adjusted !) ------------------------------------------------- */ if ( NGaps == 0 ) return( s ); NSpacesPerGap = NTrailingSpaces / NGaps; NExtra = NTrailingSpaces % NGaps; /* ------------------------------------- Construct the right-adjusted line ------------------------------------- */ outputLine = T.nextToken(); // Put first word in output for ( k = 1; k <= NGaps; k++ ) { /* ------------------------------------------- Add 1+NSpacesPerGap spaces to a gap ------------------------------------------- */ for (i = 1; i <= 1+NSpacesPerGap; i++) outputLine = outputLine + " "; /* ------------------------------------------- Add 1 more space to the first NExtra gaps ------------------------------------------- */ if (k <= NExtra) outputLine = outputLine + " "; /* ------------------------------------------- Add the next word... ------------------------------------------- */ outputLine = outputLine + T.nextToken(); } return(outputLine); } public static void printText( String[] t, int nLines ) { int i, half; System.out.println("The formatted text in 2 columns:"); System.out.println("==============================="); // ********************************* // Output lines into 2 columns // ********************************* 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 } } public static void main(String[] args) throws IOException { Scanner console = new Scanner(System.in); String OutputLine[] = new String[300]; String nextLine; int i, nLines = 0; // Read input from data file if ( args.length == 0 ) { System.out.println( "Usage: java FormatText inputFile"); System.exit(1); } File myFile = new File( args[0] ); // Open file "inp2" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file numWords = readInput( in, words ); currWord = 0; while ( currWord < numWords ) { nextLine = readOneLine( words, NCHARS_IN_LINE ); System.out.println(" |----------------------------|"); System.out.println("Before:>" + nextLine); // debug/demo use... if ( currWord == numWords ) OutputLine[nLines] = nextLine; // Last line, don't fix else OutputLine[nLines] = fixLine(nextLine, NCHARS_IN_LINE); System.out.println("After: >" + OutputLine[nLines]); // debug/demo use nLines = nLines + 1; } printText( OutputLine, nLines ); } }