Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-26-2007, 05:41 PM
Member
 
Join Date: Jun 2007
Posts: 14
bluekswing is on a distinguished road
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?



Any suggestions or help would be immensely appreciated!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-26-2007, 11:14 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
1. Define articles array like this:

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:

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

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

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

Code:
String randomResult = articles[selectedElement] + array2[random2] + ...;
Hope this helps..
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-26-2007, 11:47 PM
Member
 
Join Date: Jun 2007
Posts: 14
bluekswing is on a distinguished road
Thank you! I will be sure to try this when i get home from work.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-27-2007, 03:03 AM
Member
 
Join Date: Jun 2007
Posts: 14
bluekswing is on a distinguished road
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);
}

}
}
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-27-2007, 06:45 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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:

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

Code:
String randomSentence= articles[selectedElement] + noun1[selectedElement] + verb[selectedElement] + articles[selectedElement]+ noun2[selectedElement];
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Feature extraction from a text file in java. this is used for scoring the sentences joe_2110 Advanced Java 1 02-04-2008 09:26 PM
Random problem mcal New To Java 18 01-24-2008 05:00 AM
random numbers without random class` carlos123 New To Java 4 01-17-2008 11:44 PM
Help with Random cards carl Java Applets 1 08-03-2007 09:48 PM
vars and if sentences in XSL-FO Alan XML 1 05-31-2007 03:24 PM


All times are GMT +3. The time now is 10:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org