0

I'm creating a Mad Lib generator in Java. If you know what a Mad Lib is, this will be easier to understand. Also, I'm new to Java, so bear with me here. I want to make it so that each Mad Lib only asks for the number of adjectives, nouns, etc. that are in that specific one. For example, Mad Lib 1 may have 3 nouns, while Mad Lib 2 has 5 nouns. Is there a way to write a separate method to be run X times, creating X new strings (for the player to enter X nouns), and giving each string a different name? Just giving the strings names like nounA, nounB, nounC, or noun1, noun2, noun3 would be perfect. I need the final story to be able to differentiate between them. It will be really nice if each time I decide to add another Mad Lib to my program to just be able to loop an addNoun() method 8 times instead of writing

String nounA = scan.next();
System.out.print("\f");
String nounB = scan.next();
System.out.print("\f");

...and so on every time for each part of speech. I have not yet learned about arrays, and I think this may have a solution involving that. Like I said, I'm new to Java, so if you try to show me anything advanced, please explain it in a way that I can understand it or find a simpler way to do it.

2
  • In case you 'feel' that arrays might help here, why don't you look into that? Also check the Java Collections Framework: docs.oracle.com/javase/tutorial/collections Commented Sep 8, 2013 at 6:44
  • I looked into arrays a bit more right after posting this, which was probably not the order that I should have done that in. The answer I chose confirmed what I learned after searching, so thank you for that. Commented Sep 8, 2013 at 7:32

2 Answers 2

1

If I understand your problem correct then you need to use ArrayList and HashMap to achieve this:

First you can create ArrayList of String to store X nouns. An ex: of Arraylist is:

  List<String> nouns = new ArrayList<String>();
  nouns.add('nounA'); // add related noun
  //or
  String nounStr = 'new Noun'
  nouns.add(nounStr);

Then you can categorise particular nouns by HashMap

HashMap<String, List<String> > nounCategory = new HashMap<String, List<String> >();
nounCategory.put('madLib1', 'nouns'); // add all list with a unique key madLib1 ...

To learn more about using List and HashMap read reference List:here and HashMap:here

Sign up to request clarification or add additional context in comments.

Comments

0

If you have the count of nouns, say, in int nounCount, use arrays like this:

String[] nouns = new String[nounCount]; // Create an array
for (int i = 0; i < nouns.length; i++) {
    nouns[i] = scan.next(); // Set each item
}

Then you would be able to access different words as noun[0], noun[1], etc. You're likely to use loops and store index of selected noun somewhere though.

See also: Java arrays tutorial

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.