Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 04-05-2008, 10:26 PM
Member
 
Join Date: Apr 2008
Posts: 5
LeoCoderIV is on a distinguished road
[SOLVED] Get a random String
Hi,
i am having trouble getting a random string.
I have gotten the String from the text file and put it into an array but now i keep just getting the first line whenever i try to get a random String (which is a single line) from this array.
At the moment my code is:
Code:
public String[] generateRandomQuestion() { Random randomGenerator = new Random(); System.out.println(""); System.out.println("Your Question is : "); System.out.println(""); for(int q =1;q<2; ++q) { System.out.println( Quest[q] ); System.out.println(""); } return Quest; } }
i can see why i'm not getting a random because i havent actually asked for one but i am not sure how to do so??

Any help would be much appreciated

Thanks

Last edited by LeoCoderIV : 04-06-2008 at 11:21 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-06-2008, 12:41 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Leo, as I wrote about in your introduction, please be articulate your words in full and appropriate English, using abbreviations such as you've done is not guaranteed to be understood by all members reading your post. Writing out full English, is not only appropriate for efficient communication, but increases your chances of being understood.

Please use [ code] [ /code] tags when posting code- use the posting "preview" to verify that you've properly used the tags.

Since you've shown with two posts that you haven't completely recognized the rules of road for being a member here, I must require you to read the FAQ before you post again.

Now that that's out of the way, which could have been avoided very easily... to answer your question about using random number generators, look into using nextInt. An example run, with the code above might be:
Code:
int rand = randomGenerator.nextInt(20);
this gives you a random number between 0 and 20. A perusal of Random in the API and you can come up with many ways of doing it. Make an attempt at your custom way of doing it and we can critique or correct you on it.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-06-2008, 11:14 PM
Member
 
Join Date: Apr 2008
Posts: 5
LeoCoderIV is on a distinguished road
Sorry bad habits i guess I will work on it

Thanks for the help i will try it out and get back to you if i need to

Thanks again

Last edited by LeoCoderIV : 04-06-2008 at 11:20 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-06-2008, 11:44 PM
Member
 
Join Date: Apr 2008
Posts: 5
LeoCoderIV is on a distinguished road
ok, I tried it out but I dont think I understand fully what i need to do.

I tried:

Code:
int rand = randomGenerator.nextInt(Quest.length);


However, it doesnt make any effect on the output. Could it be that i dont have the correct variables? or i am i on the wrong tracks
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-07-2008, 04:28 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Leo, thank you for your understanding regarding the community rules. It's not something I have great joy for but it's something I committed to upon becoming a moderator. I do without a doubt enjoy assisting members with code specific issues.

I wrote a little example that has relations to your initial code. You can see the many uses of the simple line generating a random number. Remember, the parameter given to nextInt is an inclusive number in that it includes 0 up to and excluding that number. So in this case below, someone will correct me if I'm wrong.. it includes numbers 0-9. Give this a study and post any questions you may have. There's certainly more efficient ways to do this I'm sure, but I think it's sufficient for now. If you have improvements definitely post them if you want.

Code:
import java.util.Random; public class Question { String[] questions = new String[10]; public Question() { // eight prefixes to be chosen at random String [] prefix = {"What", "How", "When", "Why", "Are", "Do", "Then", "Like"}; // eight basic, first category words to be chosen at random String [] wordsOne = {"time", "day", "cool", "hungry", "is", "can't", "you", "enjoy"}; // eight basic, second category words to be chosen at random String [] wordsTwo = {"who", "awesome", "okay", "really", "turtle", "move", "look", "life"}; // eight basic, third category words to be chosen at random String [] wordsThr = {"money", "cheese", "run", "couch", "concept", "it", "cat", "factor"}; // only one suffix String suffix = "?"; Random rand = new Random(); // choose random prefixes and words to make randomly made questions for (int i = 0; i < questions.length; ++i) { questions[i] = prefix[rand.nextInt(8)] + " " + wordsOne[rand.nextInt(8)] + " " + wordsTwo[rand.nextInt(8)] + " " + wordsThr[rand.nextInt(8)] + suffix; } } public String[] generateRandomQuestion() { Random randomGen = new Random(); System.out.println("\n--Your random question is: \n" + questions[randomGen.nextInt(10)]); return questions; } public String[] generateRandomQuestions() { Random randomGen = new Random(); System.out.println("\n--Your random questions are: "); for (int q = 0; q < questions.length * 2; ++q) { System.out.println(questions[randomGen.nextInt(10)]); } return questions; } public static void main(String[] args) { Question q = new Question(); q.generateRandomQuestion(); q.generateRandomQuestions(); } }
Sample output shows this:
Quote:
--Your random question is:
Are hungry who cat?

--Your random questions are:
Then hungry okay cat?
Are hungry life run?
Are day who concept?
When you look run?
When you look run?
Are day who concept?
When you look run?
Why cool okay run?
Are hungry life run?
What you look cat?
Are day who concept?
Do is awesome money?
Then hungry okay cat?
Are day who concept?
Are hungry who cat?
Then hungry okay cat?
Are day who concept?
Do is awesome money?
Why cool okay run?
Then hungry okay cat?
Another change from the code, highlighted below:
Code:
... String[] questions = new String[20]; ... public String[] generateRandomQuestions() { Random randomGen = new Random(); System.out.println("\n--Your random questions are: "); for (int q = 0; q < questions.length; ++q) { System.out.println(questions[q]); } return questions; } ...
shows this for output, note the difference->instead of choosing random strings- outputs all randomly-made strings:
Quote:
--Your random question is:
How enjoy move factor?

--Your random questions are:
Do time turtle concept?
Do enjoy look money?
How enjoy move factor?
When you who factor?
Like can't look cat?
Why you life it?
Then can't okay it?
Do cool awesome money?
Are enjoy life it?
Do day look cat?
When hungry who cheese?
How enjoy look money?
Do day move cat?
Like you who cheese?
Are time okay money?
Then hungry okay money?
When time turtle concept?
Are day turtle it?
What can't awesome concept?
Then can't look it?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 04-07-2008 at 05:18 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-07-2008, 04:44 PM
Member
 
Join Date: Apr 2008
Posts: 5
LeoCoderIV is on a distinguished road
Thats great, thanks a lot. It works just how i need it.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-07-2008, 04:58 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Glad you found your answer here Leo - Good work Captain

In future, please mark your threads as solved. Thank you.
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
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
[SOLVED] curiosity about String type variable monir6464 Advanced Java 1 04-08-2008 01:13 PM
random string are duplicate googgoo New To Java 3 04-03-2008 12:01 PM
random numbers without random class` carlos123 New To Java 4 01-18-2008 12:44 AM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 06:59 PM
I can't seem to pass the value of a string variable into a string array mathias Java Applets 1 08-03-2007 12:52 PM


All times are GMT +3. The time now is 04:48 AM.


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