Results 1 to 19 of 19
Thread: Randomly Generate Sentences
- 10-27-2010, 01:00 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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:
Java 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 madLibsLast edited by Fubarable; 10-27-2010 at 01:15 AM. Reason: Moderator Edit: Code tags added.
-
And welcome to Java-Forums.org!
You haven't told us what your current question is, and we'll need that if we are going to have any chance of helping you. What specifically are you stuck on? What doesn't work?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:
Also, I edited your post above to add code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Again welcome!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
-
In other words, you state this:
But which bit of code above is your code attempt to solve this and how does it not work? Is it causing errors? The more you can tell us, the better we'll be able to figure out what is wrong.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.
Luck!
- 10-27-2010, 01:33 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
I'm stuck on creating a method that will take the arrays(article, verb, noun, preposition) and create a random sentence. I have started the method randomSentence. I'm stuck on just getting it working
-
- 10-27-2010, 01:45 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
i want it to take a random word out of each array and make it into a sentence. currently it just errors out. right now i can create all the arrays needed and sort them but when i try to create the random sentence nothing will happen. at this point in time it won't even compile and i'm not sure what i need to fix in it. i have tried changing some variables around but that isn't working
i don't have one variable defined atm and that is the randomlyGeneratedSentence variableJava Code: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
-
- 10-27-2010, 01:49 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
madLibs.java:37: '.class' expected is the error i get and i'm using jgrasp
-
- 10-27-2010, 02:06 AM #10
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
yes line 37
-
Please understand that line 37 means nothing to us since our line numbers don't fully correspond to yours, so your statement above doesn't help us help you. Instead, can you show me the line of code in your program above that is causing the error?
And please print out the full error message as well.
- 10-27-2010, 02:16 AM #12
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
that is the error message that i get
Java Code:madLibs.java:37: '.class' expected
Java Code:String[] randomlyGenerateSentence = randomSentence(article[], noun[], verb[], preposition[])
-
Ah OK. I've not seen a compiler state .class expected, but I can tell you that your line of code isn't valid Java. Your random sentence takes but single String array as a parameter, and you're trying to pass it four array variables. Not only that, the empty square brackets don't belong there as you only use them when declaring an array variable, not when trying to use one. If you want randomSentence to use four arrays, then you'll have to change the method to accept four arrays rather than one.
To be honest though, I'm still not sure what your code is trying to do. Perhaps it's my fault for not reading it correctly but it seems to be a bit jumbled. You've got a menu method but only call it once and after you've added words, and don't seem call the menu in a loop as one would expect, you've got an array called article[], and I'm not sure what articles are, you call articleSelection for all of your arrays, but then within the body of this method allow the user to enter different parts of speech, so they can enter adjectives when the method has been called to fill a noun array, ...
I have to ask if you planned out the program first before writing it, and if so, can you describe to us in greater detail what each step is supposed to achieve.
Luck!Last edited by Fubarable; 10-27-2010 at 02:52 AM.
- 10-27-2010, 03:03 AM #14
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
so basically here is what is going on....
this is where it accesses the articleSelection method, this will allow the user to input the type that they want. it will prompt them to enter either "noun", "verb", "adjective", or "preposition"Java Code:article = articleSelection(kb); noun = articleSelection(kb); verb = articleSelection(kb); preposition = articleSelection(kb);
it will then prompt them with how big of an array (or how many words) they want.
after they have entered the number it will then ask them to enter the words that they want. which will then store it into the according array; noun, article, verb, etc...Java Code: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
I was trying to create an array called randomlyGeneratedSentence that will take an element from each of the arrays (noun, verb, adjective, etc..) and store them into it using the randomSentence method.
after storing that array i wanted to be able to call upon that array to print out the random sentence.
i'm just working on getting the random sentence working first before i get anything else working....that is probably why it seems like it is jumbled and all over the place. i haven't really had a chance to clean it up yet...just wanting it to work atm.
does this make a little more sense?
here is the code that will work properly right now w/o the random sentence
if you want to compile it and check out what i have accomplishedJava 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); // String[] randomlyGenerateSentence = randomSentence(article[], noun[], verb[], preposition[]) 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 madLibsLast edited by kevorski; 10-27-2010 at 03:09 AM.
-
That is kind of what confuses me, and again sorry if I'm mis-reading your code, but for instance, say you want to fill the noun array with this line of code:
Java Code:noun = articleSelection(kb);
It then goes to the articleSelection method as shown here:
Java Code: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); //.....
and as you can see, this method asks the user what type of word he'd like to enter, and then fills an array of String. So while it should be only accepting nouns, the user could say that he wants to enter verbs. In my mind, the method should instead take a String parameter, perhaps called lexicalCatagory and when you call this method to collect nouns, you'd pass the String "noun" into the method, and it would prompt the user to enter nouns, and not ask him what type of words he wants to enter.
It then repeats the while loop and continues to repeat until the user enters an invalid lexical catagory, and I'm not quite sure why it does this. Why not just do the loop once if they enter a valid lexical catagory?
Also, how does the user know how many nouns, verbs, etc he wants to enter? Also, what part of speech is an "article"?
- 10-27-2010, 04:28 AM #16
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
article as my teacher defines it is:
the
a
some
many
etc
the user will enter how many words it wants to enter in the fillArray method.
this stores a temporary array named tempArray as the string array, after that temp will then become what tempArray was and then it will return the array to verb or noun or whatever was chosen.Java Code:String[] tempArray = fillArray(kb);
basically what is happening in the articleSelection method is it is matching whatever is typed and filling that array.
now that i'm looking at it, it seems that it will just go in order and no matter what i enter it will be stored as article then noun then verb and so on. how would i store an array named article for actual articles?
would you like me to post the instructions on my next post?
- 10-27-2010, 04:34 AM #17
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
So these are the instructions:
PROGRAM SPECIFICS
Write a program that reads a series of strings from the keyboard.
The strings will be stored in dynamically(No wasted space) allocated arrays. (If you want you can store all the strings in one array but it is harder)
You must first prompt for the type of string, (article, noun, verb, preposition) and then the number of strings that will be entered.
Sample input is below
Please enter the string type: art I am sorry art is not a valid type. Valid types are (article, noun, verb, preposition) Please enter the string type: article
How many articles: -5 I am sorry you must have at least one article How many articles: 0 I am sorry you must have at least one article How many articles: 5
Please enter the 5 articles one per line the a one some any
Please enter the string type: article I am sorry you already entered your articles Please enter the string type: noun
How many nouns: 3
Please enter the 3 nouns one per line boy girl dog
Input would repeat for verbs – here are 4 verbs to use drove jumped walked skipped
Here are 5 prepositions to use to from over under on
Once you the strings read in you will need to display a menu with the following choices
1) Generate a Sentence
2) Display all words
3) Add a word
4) Delete a word
5) Display all words sorted
6) Exit the program
A random sentence is made from noun verb preposition article noun and will be stored in a new allocated array
The first character in the random string may not be a letter, in which case you don't have to capitalize it. If it is a letter make it uppercase. If it already is an uppercase letter you don't have to do anything.
There will be at least one of each type .
The main function will not contain any real processing, just method calls, and a do while or some kind of loop.
The arrays will be sorted. Meaning the strings are sorted in alphabetical ascending order.
Appropriate error checking for menu choices is a must.
You must use string arrays, you can’t use ArrayList or any of those kinds of classes.
SAMPLE RUN
Welcome to the Random Sentence Generator (Stu really needs to get a life)
Please enter the string type: art I am sorry art is not a valid type. Valid types are (article, noun, verb, preposition) Please enter the string type: article
How many articles: -5 I am sorry you must have at least one article How many articles: 0 I am sorry you must have at least one article How many articles: 5
Please enter the 5 articles one per line the a one some any
Please enter the string type: article I am sorry you already entered your articles Please enter the string type: noun
How many nouns: 3
Please enter the 3 nouns one per line boy girl dog
Please enter the string type: verb
How many verbs: 4
Please enter the 4 verbs one per line drove jumped walked skipped
Please enter the string type: preposition
How many prepositions: 5
Please enter the 5 prepositions one per line to from over under on
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 1
Dog jumped over some girl
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 2
Articles: a any one some the Nouns: boy dog girl Verbs: drove jumped skipped walked Prepositions: from on over to under
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 3
Add a word to: 1) Articles 2) Nouns 3) Verbs 4) Prepositions Choice --> 3
What word would you like to add: ran
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 2
Articles: a any one some the Nouns: boy dog girl Verbs: drove jumped ran skipped walked Prepositions: from on over to under
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 1
Boy ran to some girl
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 4
Add a word to: 1) Articles 2) Nouns 3) Verbs 4) Prepositions Choice --> 3
What word would you like to delete: ran
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 2
Articles: a any one some the Nouns: boy dog girl Verbs: drove jumped skipped walked Prepositions: from on over to under
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 5
a any boy dog drove from girl jumped on one over skipped some the to under walked
Please choose from the following: 1) Generate a Sentence 2) Display all words 3) Add a word 4) Delete a word 5) Display all words sorted 6) Exit the program Choice --> 6
Have a nice day!:-)
sorry that it isn't edited it is from a pdf that my teacher sent us
-
OK, got it.
But how will the user know how many to fill?the user will enter how many words it wants to enter in the fillArray method.
Again, you could change the articleSelection method to wordSelection(Scanner kb, String lexicalCatagory), and then prompt the user to enter the type of word specfied by the lexicalCatagory String:this stores a temporary array named tempArray as the string array, after that temp will then become what tempArray was and then it will return the array to verb or noun or whatever was chosen.Java Code:String[] tempArray = fillArray(kb);
basically what is happening in the articleSelection method is it is matching whatever is typed and filling that array.
now that i'm looking at it, it seems that it will just go in order and no matter what i enter it will be stored as article then noun then verb and so on. how would i store an array named article for actual articles?
Java Code:System.out.print("Please enter " + lexicalCatagory + " type of words.");
That would probably help. I'm going to go to bed though but likely someone else will take over and help you a bit.would you like me to post the instructions on my next post?
Much luck and again, welcome!
- 10-27-2010, 04:40 AM #19
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Help. Find a word and located sentences in the file
By rashad in forum New To JavaReplies: 1Last Post: 02-28-2010, 03:06 PM -
Randomly generate a range of Ascii Characters
By Jamison5213 in forum New To JavaReplies: 2Last Post: 12-20-2009, 02:48 AM -
How to read a letter instead of a character from a given sentences
By lclclc in forum New To JavaReplies: 8Last Post: 09-15-2009, 11:53 AM -
Creating random sentences
By bluekswing in forum New To JavaReplies: 4Last Post: 06-27-2007, 05:45 PM -
vars and if sentences in XSL-FO
By Alan in forum XMLReplies: 1Last Post: 05-31-2007, 02:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks