import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class FormatText3 { 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 main(String[] args) throws IOException { if ( args.length == 0 ) { System.out.println( "Usage: java FormatText3 inputFile"); System.exit(1); } File myFile = new File( args[0] ); // Open file "inp2" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file String nextLine, fixedLine; // Read input from data file numWords = readInput( in, words ); currWord = 0; while ( currWord < numWords ) { nextLine = readOneLine( words, NCHARS_IN_LINE ); fixedLine = fixLine(nextLine, NCHARS_IN_LINE); System.out.println(" 012345678901234567890123456789"); System.out.println("Before:>" + nextLine); // debug/demo use... System.out.println("After: >" + fixedLine + "\n"); } } }