Results 1 to 5 of 5
Thread: Creating random sentences
- 06-26-2007, 04:41 PM #1
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
Creating random sentences
Greetings! I'm new to the forum but wanted to jump right in and ask some questions. :)
I'm currently taking a java class and have come across a project that actually has been stumped. The program we have been asked to create should generate 10 random sentences. The sentences should have the structure
article + noun1 + verb + article + noun2
Here's my idea of how to do it using using pseudocode:
List1 of articles = (a, the, an)
List2 of Nouns1 = (cat, dog, rat, bat, sun)
List3 of Verbs = (go, run, sit, catch, hit)
List4 of Nouns2 = (cars, store, game, baseball, chair)
for (i=1; i<=10 ; i++)
{System print (Random element from list1 + Random element from List2 + Random elemtn from List3 + Random element from List4)}
My problem is I don't know the syntax for "grabbing random element from the list and printing it where needed". I also don't know how to create arrays yet so I wondering if there are ways to store multiple values to one variable without having to create one? :confused:
Any suggestions or help would be immensely appreciated! :) :)
- 06-26-2007, 10:14 PM #2
1. Define articles array like this:
2. Define other arrays in a similar way.Java Code:String[] articles = {"a", "the", "and"};
3. Get the size of an array in the following way:
4. Create a random number between 0 and articles.length in the following way:Java Code:articles.length
5. Access to the random element in the following way:Java Code:Random r = new Random(); int selectedElement = r.nextInt(articles.length);
6. Combine these random elements in the following way:Java Code:articles[selectedElement]
Hope this helps..Java Code:String randomResult = articles[selectedElement] + array2[random2] + ...;
- 06-26-2007, 10:47 PM #3
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
Thank you! I will be sure to try this when i get home from work. :)
- 06-27-2007, 02:03 AM #4
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
Ok so here's what I've come up with, but it's not compiling and I know it's because my syntax isn't correct, but alas, I can't figure out why. Also I know I know I don't have the "articles[selectedElement]" correct either. Greatly appreciate any insights into the error of my ways. :)
import java.util.*; //grants access to outside java utilities.
import java.util.Random;
public class RandomGeneratedSenteces {
public static void main (String[] args) { //start of program.
int i;
String[] articles= {"a", "the", "an"};
String[] noun1= {"cat", "dog", "sun", "rain", "boot"};
String[] verb= {"sit", "catch", "stab", "chase", "cuddle"};
String[] noun2= {"summer", "gun", "chair", "bag", "woman"};
Random r = new Random();
int selectedElement = r.nextInt(articles.length);
for (i=1; i<=10; i++)
{String[] randomSentence= articles[selectedElement] + noun1[selectedElement] + verb[selectedElement] + articles[selectedElement]+ noun2[selectedElement];
System.out.println ("sentence " + randomSentence);
}
}
}
- 06-27-2007, 05:45 PM #5
Hello bluekswing,
Next time please use [code] tag around your codes. It is more readable and safe.
Here is a problem i saw in your code:
This cant be used like that. Write this instead:Java Code:String[] randomSentence= articles[selectedElement] + noun1[selectedElement] + verb[selectedElement] + articles[selectedElement]+ noun2[selectedElement];
Java Code:String randomSentence= articles[selectedElement] + noun1[selectedElement] + verb[selectedElement] + articles[selectedElement]+ noun2[selectedElement];
Similar Threads
-
Feature extraction from a text file in java. this is used for scoring the sentences
By joe_2110 in forum Advanced JavaReplies: 1Last Post: 02-04-2008, 08:26 PM -
Random problem
By mcal in forum New To JavaReplies: 18Last Post: 01-24-2008, 04:00 AM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
Help with Random cards
By carl in forum Java AppletsReplies: 1Last Post: 08-03-2007, 08:48 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