|
To get a sense of the problem, use a concrete example:
|
Add the first word to the line as long as the next word fits on the line { Add the next word to the line; } |
String[] words = new String[10000]; int numWords = 35 word[0] = "WE" word[1] = "hold" word[2] = "these" word[3] = "Truths" word[4] = "to" word[5] = "be" word[6] = "self-evident," word[7] = "that" word[8] = "all" word[9] = "Men" word[10] = "are" word[11] = "created" word[12] = "equal," word[13] = "that" word[14] = "they" .... (and so on) |
Add first word: 012345678901234567890123456789 line = We| |
|
|
![]() |
|
Ideal implementation in method form:
/* ------------------------------------------------------------ The readOneLine() method must update the currWord parameter We must pass the parameter currWord by-reference ! ------------------------------------------------------------ */ public static String readOneLine( int currWord, String[] w, int maxLineLength ) { String line; // Line with as many words as possible line = w[ currWord ]; // Add current word as first word currWord = currWord + 1; // Get next word while ( currWord < numWords && line.length() + 1 + w[currWord].length() <= maxLineLength ) { // Next word fits; add next word to line line = line + " " + w[ currWord ]; // Add next word to line currWord = currWord + 1; // Get next word } return( line ); } |
Explanation:
|
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 ); } /* ================================================================== readOneLine(w, maxLineLength): get one line of text from w[] (<= maxLineLength characters) Starts getting words from w[currLine] =================================================================== */ 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); } /* ======================================= main(): test the readOneLine() method ======================================= */ 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"); } } } |
Example:
|
How to run the program:
|
public static String[] words = new String[10000]; // words[] hold words from input public static int currWord; // Current word in words[] public static int numWords; // # words in words[] |
|
So we also define the other related variables as class variable...
(BTW, words and numWords could have been defined as local variables).