Randomly Generate Sentences
Hello everyone.
First off I would like to say that this website looks great and will come in handy for my Java class.
So I've been working on this homework the last few days;
We are creating a mad libs game. Right now I'm stuck on randomly creating a sentence from the user input. I currently have the user input arrays and just need to be able to call them from a method.
Any help would be awesome, thanks in advance!
This is what I currently have:
Code:
import java.util.Random;
import java.util.Scanner;
public class madLibs
{
public static void main(String [] args)
{
String[]article, noun, verb, preposition, randomlyGeneratedSentence = null;
String str = "";
int n;
Scanner kb = new Scanner(System.in);
article = articleSelection(kb);
noun = articleSelection(kb);
verb = articleSelection(kb);
preposition = articleSelection(kb);
n = menu(kb);
if(n == 1)
System.out.println("Your random sentence is: " + randomlyGeneratedSentence);
else if(n == 2)
{
System.out.println("The articles are: " + displayAllWords(article));
System.out.println("The nouns are: " + displayAllWords(noun));
System.out.println("The verbs are: " + displayAllWords(verb));
System.out.println("The prepositions are: " + displayAllWords(preposition));
}
}//end main
public static int randomSentence(String[] array)
{
int i;
Random r = new Random();
int selectedElement = r.nextInt(array.length);
return selectedElement;
}//end randomSentence
public static String[] displayAllWords(String[] array)
{
for(int x=0; x<array.length; x++)
{
System.out.println(array[x]);
}
return array;
}//end displayAllWords
public static String[] articleSelection(Scanner kb)
{
String s = "";
String[] temp = null;
do
{
System.out.println("Please enter your string type (article/noun/verb/preposition): ");
s = kb.nextLine();
if(s.equals("article"))
{
String[] tempArray = fillArray(kb);
temp = tempArray;
}
else if(s.equals("noun"))
{
String[] tempArray = fillArray(kb);
temp = tempArray;
}
else if(s.equals("verb"))
{
String[] tempArray = fillArray(kb);
temp = tempArray;
}
else if(s.equals("preposition"))
{
String[] tempArray = fillArray(kb);
temp = tempArray;
}
else
{
System.out.println("Please enter a valid string type");
s = kb.nextLine();
}
}while (!s.equals("article") && !s.equals("noun") && !s.equals("verb") && !s.equals("preposition"));
return temp;
}//end articleSelection
public static String[] fillArray(Scanner kb)
{
int num;
String[] temp;
System.out.println("How many words would you like to enter?: ");
num = kb.nextInt();
kb.nextLine();
while(num<1)
{
System.out.println("Please enter a number greater than 0: ");
num = kb.nextInt();
kb.nextLine();
}
temp = new String[num];
for(int x=0; x<temp.length; x++)
{
System.out.println("Please enter your word: ");
temp[x] = kb.nextLine();
}
return temp;
}//end fillArray
public static int menu(Scanner kb)
{
int choice;
System.out.println("Welcome to the mad libs game!");
System.out.println("1) Randomly generate a sentence");
System.out.println("2) Display all words");
System.out.println("3) Add a word");
System.out.println("4) Delete a word");
System.out.println("5) Display all words sorted");
System.out.println("6) Exit the program");
System.out.println("Please make a selection: ");
choice = kb.nextInt();
if(choice<1 || choice>6)
do
{
System.out.println("Please enter a valid selection: ");
choice = kb.nextInt();
}while(choice<1 || choice>6);
return choice;
}//end menu
}//end madLibs