My First Java Program: Mad Libs Haiku Generator

So, today was my second day of OOP class. We’re starting out in Java; naturally, our first programs were a bunch of print statements with names like “” and “”.

After figuring out three different ways of creating the correct output for the assigned demo program, I decided to spend the remainder of today’s lab time writing something a little more exciting than a print statement — something I might actually screw up and have to debug.

It’s a Mad Libs-style haiku generator that uses the Java Scanner class to get user inputs and returns a dorky, three-line poem.

If you’re all Java-happy over there on your end, you can right-click to download the compiled byte code and run it.

Here’s what it looks like:

//Mad Libs Haiku

import java.util.Scanner;

public class Haiku
   public static void main( String[] args )
   //create Scanner to get input from user
      Scanner input = new Scanner( );

      String word1;//Person's name, 2 syllables
      String word2;//Noun, 2 syllables
      String word3;//Adjective, 1 syllable
      String word4;//Adjective, 1 syllables
      String word5;//Adjective, 2 syllables

      System.out.print( "Type a person's name -- two syllables: " );//prompt
      word1 = input.nextLine();//read user input

      System.out.print( "Type a two-syllable noun: " );//prompt
      word2 = input.nextLine();//read user input

      System.out.print( "Type a one-syllable adjective: " );//prompt
      word3 = input.nextLine();//read user input

      System.out.print( "Gimme another one-syllable adjective: " );//prompt
      word4 = input.nextLine();//read user input

      System.out.print( "Last one! A two-syllable adjective: " );//prompt
      word5 = input.nextLine();//read user input

      System.out.println( "\n\nHere is your Mad Libs-style haiku:\n\n" );
      System.out.printf( "I said to %s,\n", word1 );
      System.out.printf( "\"My love is like a %s:\n", word2 );
      System.out.printf( "%s, %s, and %s.\"\n", word3, word4, word5 );

   }//end method main
}//end class Haiku

I realize there’s a potential a/an problem, but I’m not yet equipped to solve it. I got the basic idea for this code from a textbook example for a simple addition app that took user inputs for two integers and returned the sum. If there are egregious errors and/or examples of flagrant stupidity, be sure to let me know about them in the comments.

Other notes: I am allergic to chalk dust, apparently. I like sitting in the front row, which is (duh) nearest the chalkboard. Within 15 minutes of the lecture starting, I was sneezing like a third grade class in November. I’ll be taking allergy meds for the rest of the semester.

13 thoughts on “My First Java Program: Mad Libs Haiku Generator

  1. Hmm the only thing I can think of is that there is nothing that prevents the user from just hitting enter at any prompt. Unless nextLine() does that automatically. I’m a C# developer, so my Java-fu is weak.

  2. Congrats Jolie! Welcome to the world of programming, where many heads bang on walls, fists smash keyboards, and mice fly across computer labs.

    But on a more serious note, very glad to see more people taking interest in gaining skills in software development skills. Kudos on your initiative and effort! Keep at it and I look forward to seeing you improve at this and eventually I’m sure be as good a developer as you are a writer πŸ™‚ ..

    • Thanks, dude! That’s the goal.

      For quite some time, I’ve felt that *just* being a good writer isn’t enough to justify my existence in the tech world. I’d rather do something that really contributes, you know?

      • I think that’s amazing – really shows your dedication to your profession. I saw earlier that you were trying to learn Python as well so obviously you’re taking this very seriously.

        Wish you the best and don’t give up – many times programming can be very discouraging. Up at 3am and see 50 bugs that you can’t fix. But that satisfaction of fixing all of that and creating a final product can’t compare to that irritation. I’m in University doing Computer Science so I’ve had many of those nights, but I always love the end result. I’m sure your other developer friends have told you the same!

  3. Been developing in Java for more then 8 years now and have never used Printf. The fun of programming is, everyday you learn something new from everyone.

    Glad to see Haiku program, next stop Jaiku program πŸ˜‰

  4. Great starters Jolie – Glad to see you’re doing your first steps into the prgramming world, which is fun despite all the headache it can cause. πŸ˜‰

    The fun thing about it is that you’ll always learn new things, always find better ways to do things, including redesigning your own developments.

    Tip (you’ll have to find the solution yourself) for your “a/an” problem:

    The logic says: check if the first letter of the word is a voyel (a,e,i,o,e,y), you should print a “a” otherwise you print a “an”. Easy way: Think about storing the “a” or “an” into a variable that is printed the rest of your output. πŸ˜‰

    I am sure you may now improve your program.


  5. Hey Jolie!!!
    So cool to see your first program! The fact that you took the curiosity and interest in turning something as boring as print statements into a really neat, actually has a purpose, program tells me that you have the likings of a programmer hidden inside you. Good job! Keep it up!


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s