import java.io.*; import java.util.Scanner; public class FormatText2 { 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); } public static void main(String[] args) throws IOException { if ( args.length == 0 ) { System.out.println( "Usage: java FormatText2 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; // Read input from data file numWords = readInput( in, words ); currWord = 0; while ( currWord < numWords ) { nextLine = readOneLine( words, NCHARS_IN_LINE ); System.out.println(" 012345678901234567890123456789"); System.out.println(" >" + nextLine + "\n"); } } }