Results 1 to 7 of 7
  1. #1
    LeoCoderIV is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Talking [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:
    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;
        }
          
            }
    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 09:21 PM.

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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:
    Java 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.
    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)

  3. #3
    LeoCoderIV is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    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 09:20 PM.

  4. #4
    LeoCoderIV is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    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

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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.

    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();
      }
    }
    Sample output shows this:
    --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:
    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;        
      }
    ...
    shows this for output, note the difference->instead of choosing random strings- outputs all randomly-made strings:
    --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)

  6. #6
    LeoCoderIV is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Thats great, thanks a lot. It works just how i need it. :D

  7. #7
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    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 me! :cool:

Similar Threads

  1. [SOLVED] curiosity about String type variable
    By monir6464 in forum Advanced Java
    Replies: 1
    Last Post: 04-08-2008, 11:13 AM
  2. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 10:01 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. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

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