Littlesims2chick
Littlesims2chick

Reputation: 1

How do I use the randomizer in a Java Mad Libs Program?

I'm trying to do Mad Libs but I'm having hard time trying to figure out how to get the randomizer to select a random verb, noun, or adjective from the arrays I created since they're all words, not numbers... Same thing for the String sentences.

import java.util.Scanner;
import java.util.Random;


public class Client {
    public static void main(String [] args){
        Scanner input = new Scanner(System.in);
        Random r = new Random();

        //user will enter 5 nouns
        System.out.println("Enter 5 nouns");
        String[] nouns = { "noun", "noun", "noun", "noun",
            "noun"};
        for (int ncount = 0; ncount < 5; ncount++) {
            System.out.print(nouns[ncount] + ": ");
            nouns[ncount] = input.nextLine();
        }



        //user will enter 5 verbs
        System.out.println("Enter 5 verbs");
        String[] verbs = { "verb", "verb", "verb", "verb",
            "noun"};
        for (int vcount = 0; vcount < 5; vcount++) {
            System.out.print(verbs[vcount] + ": ");
            verbs[vcount] = input.nextLine();
        }


        //users will enter 5 adjectives
        System.out.println("Enter 5 adjectives");
        String[] adjectives = { "adjective", "adjective", "adjective", "adjective",
            "adjective"};
        for (int acount = 0; acount < 5; acount++) {
            System.out.print(adjectives[acount] + ": ");
            adjectives[acount] = input.nextLine();
        }





        String sentence1 = "Longwood" +  r.verbs.length + "the" + r.adjectives.length + r.nouns.length;
        String sentence2 = "The" + noun + "in cafe3" + verb + adjective;
        String sentence3 = "The girls will" + verb + adjective + "dresses to the" + noun; 
        String sentence4 = "Students will" + verb + adjective + "at the" + noun; 
        String sentence5 = "Students get"+ adjective + "when their" + noun + "are" + "by the staff."; 
        String sentence6 = "Recieving a day of" + noun+ verb + "me" + adjective; 
        String sentence7 = "At freshman orientation students" + verb + "about the" + adjective + noun + "on"
        + "the roof"; 
        String sentence8 = "Senior" + noun + "is the place where you can" + verb + adjective +"!"; 
        String sentence9 = "The Longwood Varsity"+ noun + "team" + verb + "Long Island" + adjective + "For 2015!"; 
        String sentence10 = "The teacher of my" + noun +  "class" + verb + adjective; 

Upvotes: 0

Views: 734

Answers (1)

samczsun
samczsun

Reputation: 1024

You can make blank arrays without any values by doing

String[] array = new String[x]; //x is the size of the array

To pick a random word, you can use Random's nextInt with a bound of the array size. The random number you get will be the index of the word to use

Upvotes: 1

Related Questions