Results 1 to 7 of 7
Thread: [SOLVED] Get a random String
- 04-05-2008, 08:26 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
[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:
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??Java 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; } }
Any help would be much appreciated
Thanks :)Last edited by LeoCoderIV; 04-06-2008 at 09:21 PM.
- 04-06-2008, 10:41 AM #2
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:
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.Java Code:int rand = randomGenerator.nextInt(20);
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 04-06-2008, 09:14 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
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 againLast edited by LeoCoderIV; 04-06-2008 at 09:20 PM.
- 04-06-2008, 09:44 PM #4
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
ok, I tried it out but I dont think I understand fully what i need to do.
I tried:
Java 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 :o
- 04-07-2008, 02:28 AM #5
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.
Sample output shows this:Java 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(); } }
Another change from the code, highlighted below:--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?
shows this for output, note the difference->instead of choosing random strings- outputs all randomly-made strings:Java Code:... String[] questions = [B]new String[20];[/B] ... public String[] generateRandomQuestions() { Random randomGen = new Random(); System.out.println("\n--Your random questions are: "); for (int q = 0; q < [B]questions.length[/B]; ++q) { [B] System.out.println(questions[q]); [/B] } return questions; } ...
--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?Last edited by CaptainMorgan; 04-07-2008 at 03:18 AM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 04-07-2008, 02:44 PM #6
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
Thats great, thanks a lot. It works just how i need it. :D
- 04-07-2008, 02:58 PM #7
Similar Threads
-
[SOLVED] curiosity about String type variable
By monir6464 in forum Advanced JavaReplies: 1Last Post: 04-08-2008, 11:13 AM -
random string are duplicate
By googgoo in forum New To JavaReplies: 3Last Post: 04-03-2008, 10:01 AM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
me! :cool:
Bookmarks