Results 1 to 12 of 12
  1. #1
    iriscience is offline Member
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    11
    Rep Power
    0

    Default [SOLVED] Generate Random Letter

    Hi! I am very new to Java. This is my first time taking a class on it and for an assignment I have to make a game that generates a random number and has the player guess it within 4 tries. Unfortunately, I cannot figure out how to generate a random letter. I have been searching for around an hour and cannot figure it out! From what I have gathered, I have make a string containing my letters.

    Java Code:
    String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    Then, I am supposed to generate a random number from 0-25.

    Java Code:
    int num = (int)(Math.random() * 26);
    Then, I am supposed to assign the randomly generated number to it's letter equivalent. And this is where I'm lost. I have no idea where to go from this point. I can't seem to figure out what code I can use to assign the number to it's corresponding letter. Any help is GREATLY appreciated! Thanks so much!

    PS: I'm sorry if this has been a recurring question. I have looked in so many forums and read so many different threads, that my brain feels all squishy. So please don't hate me too much for this.
    Last edited by iriscience; 01-31-2011 at 07:28 AM. Reason: solved

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    letters[num] :confused:

  3. #3
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    The number you generated is between 0 and 25, which is perfect, because that corresponds to the elements in the array you created.

    Say the number you generated is 4, and you want this to be converted to "E" (where 0 is "A").

    Java Code:
    String choice = letters[num];
    will assign the letter at element [num] to the choice. Generate a 3, you have "D". Generate a 25, you have "Z".

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Java Code:
    String[] letters = ...
    int num = ...
    String randomLetter = letters[num];

    The expression letters[num] means "the n-th element of the letters array". See Arrays in Oracle's Tutorial, or your textbook.

    --------------------------

    "Just the place for a Snark!" the Bellman cried,
    As he landed his crew with care;
    Supporting each man on the top of the tide
    By a finger entwined in his hair.

    "Just the place for a Snark! I have said it twice:
    That alone should encourage the crew.
    Just the place for a Snark! I have said it thrice:
    What i tell you three times is true."

  5. #5
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    Here is a pretty easy way to do it. Have the String[] with all the letters then get one of them using a randomly generated number from 0 to 25. Like this:
    Java Code:
    String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    
       public static String randomLetter()
       {
          int num = (int)(Math.random() * 26);
          return letters[num];
       }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Two more and I get to quote A. A. Milne...

  7. #7
    iriscience is offline Member
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Java Code:
    String[] letters = ...
    int num = ...
    String randomLetter = letters[num];

    Thank you so much! This is exactly what I was looking for! Couldn't have been easier! Thank you! Thank you! :D

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pbrockway2 View Post
    Two more and I get to quote A. A. Milne...
    My favorite:
    "Did you ever stop to think, and forget to start again?"

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Thank you so much!
    You're welcome.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    This can be achieved without using an array for the letters. A hint:
    Java Code:
    System.out.println((char)65);
    Now all you need is to generate a random number in the correct range.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    but what A. A. Milne quote were you talking about?

    Well I was the third person to suggest accessing the array at the index that had been calculated. Hence the Snark quote.

    Then Zman3359 suggested the same thing (4).

    And I thought "Two more and ... Now We Are Six"

    ------------------

    When I was One,
    I had just begun.
    When I was Two,
    I was nearly new.
    When I was Three
    I was hardly me.
    When I was Four,
    I was not much more.
    When I was Five,
    I was just alive.
    But now I am Six,
    I'm as clever as clever,
    So I think I'll be six now for ever and ever.

Similar Threads

  1. Random integer generate
    By trbLeeciN in forum New To Java
    Replies: 6
    Last Post: 06-22-2010, 02:19 AM
  2. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  3. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 09:37 AM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:32 PM

Tags for this Thread

Posting Permissions

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