package rebelsky.exam1; import java.io.PrintWriter; import java.io.BufferedReader; /** * DESCRIPTION * * @author YOUR NAME HERE * @version V of DATE */ public class MadLib { // +--------------+-------------------------------------------- // | Class Fields | // +--------------+ // +--------+-------------------------------------------------- // | Fields | // +--------+ String contents; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ public MadLib(String _contents) { this.contents = _contents; } // MadLib(String) // +---------+------------------------------------------------- // | Methods | // +---------+ public void play(PrintWriter pen, BufferedReader eyes) throws Exception { // Make a copy of the contents so that we can modify it. String str = this.contents; // As long as "blanks" remain in the string, ask about them int firstBlank = str.indexOf("_"); while (firstBlank >= 0) { // Find the next blank int secondBlank = str.indexOf("_", firstBlank+1); // Ask the user for a value pen.print("Please enter a(n) " + str.substring(firstBlank+1,secondBlank)); pen.flush(); String response; response = eyes.readLine(); // Replace that whole block with what the user enters str = str.substring(0,firstBlank) + response + str.substring(secondBlank + 1); // And start again firstBlank = str.indexOf("_"); } // while // Print out the result pen.println(str); } // play() // +---------------+------------------------------------------- // | Class Methods | // +---------------+ } // class MadLib