Results 1 to 5 of 5
  1. #1
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default 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! :) :)

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    1. Define articles array like this:

    Java Code:
         String[] articles = {"a", "the", "and"};
    2. Define other arrays in a similar way.

    3. Get the size of an array in the following way:

    Java Code:
         articles.length
    4. Create a random number between 0 and articles.length in the following way:

    Java Code:
         Random r = new Random();
         int selectedElement = r.nextInt(articles.length);
    5. Access to the random element in the following way:

    Java Code:
        articles[selectedElement]
    6. Combine these random elements in the following way:

    Java Code:
        String randomResult = articles[selectedElement] + array2[random2] + ...;
    Hope this helps..

  3. #3
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    Thank you! I will be sure to try this when i get home from work. :)

  4. #4
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    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);
    }

    }
    }

  5. #5
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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:

    Java Code:
    String[] randomSentence= articles[selectedElement] + noun1[selectedElement] + verb[selectedElement] + articles[selectedElement]+ noun2[selectedElement];
    This cant be used like that. Write this instead:

    Java Code:
    String randomSentence= articles[selectedElement] + noun1[selectedElement] + verb[selectedElement] + articles[selectedElement]+ noun2[selectedElement];

Similar Threads

  1. Replies: 1
    Last Post: 02-04-2008, 08:26 PM
  2. Random problem
    By mcal in forum New To Java
    Replies: 18
    Last Post: 01-24-2008, 04:00 AM
  3. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  4. Help with Random cards
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-03-2007, 08:48 PM
  5. vars and if sentences in XSL-FO
    By Alan in forum XML
    Replies: 1
    Last Post: 05-31-2007, 02:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •