import java.io.*;

public class MadLib {

    public static void main (String [] args) throws IOException {
	// Setting up the reading:
	// Create a buffered reader
	BufferedReader in = new BufferedReader(
			new InputStreamReader(System.in));
	// to read a line of input, use in.readLine();
	String input;

	// set up the text:
	String text = new String("There once was a ADJ1 PERSON");
	text = text.concat(" who lived in a ADJ2 OBJECT");
	text = text.concat(".\nThe OBJECT was so ADJ2 that on rainy days");
	text = text.concat(" PERSON had to VERB. The end.");

	System.out.print("Please enter a person or an animal: ");
	input = in.readLine();
	text = text.replaceAll("PERSON",input);
	System.out.print("Please enter an object: ");
	input = in.readLine();
	text = text.replaceAll("OBJECT",input);
	System.out.print("Please enter an adjective: ");
	input = in.readLine();
	text = text.replaceAll("ADJ1",input);
	System.out.print("Please enter another adjective: ");
	input = in.readLine();
	text = text.replaceAll("ADJ2",input);
	System.out.print("Please enter a verb: ");
	input = in.readLine();
	text = text.replaceAll("VERB",input);

	System.out.println("\nHere is the story:\n");
	System.out.println(text);
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.