import java.io.*; import java.util.Vector; /** * A simple class for formatting text messages. * * This class illustrates the use of the Vector * class. * * @author Prof. David Bernstein, James Madison University * @version 2.0 (Using type-safe generics) */ public class TextFormatter { /** * The entry point of the program * * @param args The command-line arguments */ public static void main(String[] args) { BufferedReader in; int displayWidth, i, width; String word; Vector message; // v2.0 in = new BufferedReader(new InputStreamReader(System.in)); // Initialization displayWidth = 20; // Construct a new Vector message = new Vector(); // v2.0 // Prompt the user to enter the message // one word at a time do { word = ""; System.out.print("Next word: "); try { word = in.readLine(); message.add(message.size(), word); } catch (IOException ioe) { ioe.printStackTrace(); } } while (!word.equals("")); System.out.print("\n\n\n"); // Format the message so that it fits in // a fixed-width display width = displayWidth; for (i=0; i < message.size(); i++) { word = (message.get(i)); // v2.0 if (width+word.length()+1 > displayWidth) { width = word.length(); System.out.print("\n"); System.out.print(word); } else { width = width + word.length() + 1; System.out.print(" "+word+" "); } } } }