Mad Libs Clone Project

Start course
2h 7m

This course explores the fundamentals of Java and puts them to use with some real-life examples looking at an average of three program and a mad libs project.

Learning Objectives

  • Learn how to print information out to the user, how to create and use variables, values, and constants, how these things have data types, and the differences and similarities among the data types
  • Learn about arithmetic, relational, and logical operators
  • Understand how to obtain input from the user of our programs

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java


This is a beginner-level course and can be taken by anyone with an interest in learning about Java.


In the last lecture, we worked on our first section project, averaging three numbers. This helped us expand our arithmetic abilities in Java. In this lecture, we will solidify some of our skills for working with strings. If you aren't familiar with them, Mad Libs are a great game that usually are in a book and are fun for pretty much all ages. They are great to keep kids busy while traveling as well. Someone will be asked to provide words of different kinds like names, occupations, clothing, adjectives which describe noun so examples would be blue, fat, tired, smelly, smaller anything like that. Or really any host of possible words. Then when the person has filled in or otherwise provided all the words that are requested, they are placed in blanks in a pre-written story and lots of funny and bizarre stories are created as a result. We'll be writing a simple clone of a single Mad Libs game. We will be prompting the user before they see the story for various words and parts of speech.

You can collect all this data as strings. Then you place these words throughout the story in the appropriate spots indicated. So, you will create a file named proj2_2_MadLibsClone and complete this project by prompting the user for different strings and then printing the full story to them after you're done. The story is available for you right now. And the story goes like this. There once was a adjective1 girl named girls_name, who was a adjective2 in the kingdom of place name. She loved to wear some sort of clothing and to hobby. She wanted to marry the adjective occupation named boys_name. But her father, King mans_name forbid her from seeing him. All right. Let's run this program so you can see how it works and then maybe that'll make it easier to work on it. So, we right click here on the 'proj2', I'm not showing you the code of course, but proj2_2_MadLibsClone, right click, and then we're going to go into the run and there it is, enter an adjective so I might put something like big girl's name is Susan.

Another adjective will say skinny plumber, name of a place, we'll say Delhi, and then name of a piece of clothing, we'll say dress or dresses I think would be better. Name of a hobby, let's say she likes to swim. And then enter another adjective, we'll say orange. I will say occupation is firefighter. Boy's name is Billy. Man's name is Randy. Okay. And there we go. There once was a big girl named Susan who was a skinny plumber in the Kingdom of Delhi. She loved to wear dresses and to swim. She wanted to marry the orange firefighter named Billy but her father king Randy forbid her from seeing him. So, hopefully that little silly example hopefully gives you an idea of how the program is supposed to function. So, pause the video, come back when you're done or if you need some help. How did that go for you? Were you able to complete this Mad Libs clone project? It's totally fine if you had trouble or found it challenging.

Don't worry. You will get better with every line of code you write and every line of code you attempt to write. If you were able to complete it, congratulations. That's awesome. Keep going strong. Let's work on this project together. So, let's create the appropriate file. We will right-click 'Source', 'New', 'Java class', and I will call this proj2_2_MadLibsClone. And then what we're going to do is we're going to have the scanner imported, java.util.scanner. And then of course we have our main method, public static void main. Good. Now, inside here we have quite a bit of stuff that we need. We're going to do this, we need a scanner which we'll call keyboard. And again, there's nothing magical about that name. It just seems like a legitimate and reasonable name for the scanner. Taking input from the standard input device, in this case the keyboard. Now, here are the strings we need.

So, there's quite a bit of strings and you could have looked at the story and pulled them out. I recommend not using all caps like I did in the story. That was just so they would pop easily from the story and you could come up with good names, good identifiers because these aren't constants so you really shouldn't capitalize them. So, adjective1, string girls_name, string adjective2, string occupation1, string place name, string clothing, string hobby, string adjective3. And then occupation2, string boysName, and string mansName. If you need to pause the video again and copy what I've done, that's totally fine too. At any point, you can pause the video and that's one of the advantages of taking a course this way. In an asynchronous fashion so you can pause whenever you need to. Now, we have to ask for each of these. So, this is a little bit of busy work, but it's really not that hard. System.out.println enter an adjective.

Actually print, we'll do print on this. I'm going to make it a little bit cleaner here. We'll do print and then put a tab so that we'll use the tab character. We'll tell them to enter an adjective. And since these are all strings, we don't have to worry about consuming new line characters or anything like that. We just use it as is. Adjective1 equals keyboard.nextLine. Here we go. So, that's our first thing we're asking for is the adjective one. And then I'm going to use the shortcut here. Sout, just to show you that there is a shortcut, And that's the same in, say, IntelliJ and that means I think Eclipse, but I don't use Eclipse that much. So, system.out.print, you just have to take it back to print instead of print line. Enter a girl's name with the tab. And then we have a girl's name keyboard.nextline. Good. Next system.out.print. Enter another adjective and your prompts may be very different from mine and that's fine.

Next line. Okay. Adjective two, and then we're going to say system.out.print, we'll print, sorry keep going to print line because I'm used to using that. Enter an occupation. They don't have to be specific parts of speech. You can just say really specific things like here's an occupation or I would like an occupation. Now, the idea is you kind of got a sneak peek here and you know what the story looks like, so maybe it's not as impressive to you. But the idea is that if someone were to just see the blanks basically or be asked all these pieces of information, they don't know where it's going or what kind of story it is and that can lead to some pretty, pretty funny stories. So, enter an occupation and we grab the occupation, then we have system.out.print, and we will say enter the name of a place, and then that's a tab also, and then we have place name is keyboard.nextline, next line, here we go.

And after the place name, we will say system.out.print and I will say enter the name of a piece of clothing, and clothing equals keyboard.nextline and then we have this system.out.print. The enter the name of a hobby, backslash T. And then hobby equals keyboard.nextline. And then we have almost done. Don't worry, sometimes coding takes a lot of time. Enter name of a hobby. And then we have enter another adjective backslash T. And then adjective3 equals keyboard.nextline. And then, we have a few more so after we've grabbed that third adjective, we then have to move onto the second occupation. So, we'll say, System.out.print and then  ("Enter another occupation:\t");  Here we go. 

And then we've got just a couple more, System.out.print, so we got that prompt,  ("Enter a boy's name:\t"). And we get boysName = keyboard.nextLine(). And then we have System.out.print("Enter a man's name"). mansName = keyboard.nextLine(). Now, we have the story to take care of. So, this is a little bit longer, but once we're done with it, should be good. So, this one will do println("There once was a ",  make sure you get the space, and then we are going to put adjective1.  So, it's adjective1 and then we also need girl. So, we need the adjective1 girl. I need my space there. And then I guess we can put named, and then you can put girlsName + " who was a ". Go to next line here, and then this is adjective2. Then you do need a space, and then occupation1, then " in the Kingdom of  " and placeName and a period. Now I will actually do another println here. It doesn't really matter. You can do it all at once. We could say, ("She loved to wear " + clothing +. I hope so, but this is a specific piece of clothing. " and to ", now into hobby. This could be a little bit confusing. So, you might need to actually put clarifying remarks when you prompt for it because if someone says sewing, then and likes to and to sewing, that doesn't really make much sense. She might have say like hobby, present tense or something like that. And a period, then she wanted to marry the, then next line. We've got adjective3, and then we have a space, occupation2. And then this one is " named " + boysName  + " but her father, King " + and then we have mansName. Next line. Space " forbid her from seeing him. ") I think I got it right. Remove some of the extra spaces.

Looks pretty good to me. Okay, let's play it. So, I'm going to right click, and we're going to run this app. That was quite a bit of typing. Right click and then 'Run 'Proj2_2_MadLibsClone.main()''. Bring this up here. Enter an adjective. Okay, let's say, blue; girl's name: Sally; another adjective, we'll say, fat; and occupation: plumber. Enter the name of a place. Let's say, got to go with Detroit. Let's say, piece of clothing; now that one should have been just a print instead of a println. I think I goofed on that one. See, there's a println right there. See, you can recognize an error right away. We'll fix it in a minute or just leave it; doesn't really matter. Enter the name of a piece of clothing. We'll say... hat and I'm trying to think she loved to wear hat. So, let's say, hat; maybe again, that would be something that might require some sort of clarification like plural; make sure it has a plural word. Enter the name of a hobby. We'll say, sewing. Enter another adjective. So, this is a modifier of a noun again. So, another adjective, we'll say, fast. Another occupation, we'll say, farmer; and then a boy's name; that would be Bob, and then a man's name also missed a space. So, there's a couple little things we need to fix here. The name should be, we'll say, Sam. Okay, let's see if this looks good.

There once was a blue girl named Sally who was a fat plumber in the Kingdom of Detroit. She loved to wear hats and to sewing. See, that was a problem. So, that's what we should have done. She wanted to marry the fast farmer named Bob but her father, King Sam forbid her from seeing him. So, we do have a couple of things we can go back and fix. Enter a man's name; we know there's a little issue there. So, let's put the colon in a tab, and then up here, instead of println for the clothing, we will do print. So, I think that takes care of those problems. You would, of course, run it over and over again and make sure that this works and looks good.

But I think you get the point. You might also want to put an additional space at the end before the story starts. So, you would put something like this and that just prints a blank line. So, did a good job. Maybe in informal challenge, you can make more Mad Libs and try them out on your friends and family. That would be a really cool and formal challenge. In the next lecture, we will wrap up this section. I will see you there.


About the Author
Learning Paths

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics