CIT 591 Assignment 5: Mad Libs
Fall 2004, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Create a "Mad Lib." If you aren't familiar with this silly children's game, take a look at Wacky Web Tales ( before reading further.


Create a text file containing a story, with "blanks" (actually, terms in brackets) to be filled in. Here is an example, "The Best Day of My Whole Life," from the Wacky Web Tales site mentioned above. I chose this one because it has relatively few blanks to fill in.

One day I was walking to school when I found a wallet
on the ground. I picked it up. It had no ID in it, so
I decided to keep it. It had [large number] bucks in it.
Like any kid, I was [adjective].

The first thing I did was go to the police to check if
anybody lost it. The man said he would keep it in case.

I was, of course, [feeling]. Then after a few days of
being happy, I got a call from the person who lost the
wallet. He said he was really [feeling] with me for being
honest. He was so grateful that he sent my family and me
on a trip to [foreign country].

It was the most [adjective] day of my life!

Save your (original) story on a text file, not a formatted file such as MSWord produces. For our convenience, name your file story.txt.

Your program should read this file, one line at a time, into an ArrayList. Then it should search each line in the ArrayList for any text within brackets, ask the user for a word or words of that kind (don't show the brackets), and replace them in the text. For each question, use "a" before a consonant, "an" before a vowel; for example, if you are asking for an adjective, ask for an adjective, not for a adjective.

When everything has been filled in, print the story.

No, we haven't talked about ArrayLists. They are objects that you can use like arrays (though you can't use any of the special array syntax, such as a[i]). The big advantage of an ArrayList, compared to an array, is that you don't need to know in advance how big to make it. Look up ArrayList in the Java 1.4.2 API in java.util. (Note: If you look in the Java 1.5 API instead , you will see a lot of new stuff, using angle brackets, such as <E> or <? extends E>. Ignore everything with angle brackets--we aren't using Java 1.5 yet.)

You can assume:

You can not assume:

Name your class MadLib. Your main method should call a constructor to create a new MadLib object, then call this new object's play() method. The constructor should read in and save the story text; your play() method should ask the user questions and display the finished story. You may write as many additional methods as you need; in particular, if you find yourself repeating code, you should try to put that code into a method.

To ask the user for a word or phrase of a particular kind, import javax.swing.*, and use this code:

answer = JOptionPane.showInputDialog(null, question);

where question and answer are both of type String. Check to make sure that the user actually typed something--insist on getting a response to every question.

To read lines from a simple text file, use my LineReader class, in You can look at the javadoc documentation for it here, or you can simply choose "Interface" in your BlueJ editor window.


This is another "do it yourself" project. It's a good idea to work with someone while you're in the lab, and (as always) please help each other in various ways--but write this program yourself. Don't copy or share code. Everyone gets a separate grade.

Coding style (indentation and spacing) will be considered in grading this project. See the Java™ Style Guide for Sun's official rules, at

Due date: Zip up your complete BlueJ project, and turn it in via Blackboard before midnight, October 21.