Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Random Letter problem please help!

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Random Letter problem please help!

    I've got a database of words which are held in an arraylist. I have created a getWord method which selects a word at random. The computer needs to select a letter out of the seleted random Word. At the moment it is selecting a letter from A-Z which is not correct. I would like it to select a letter from the chosen word, similar to hangman.



    private void chooseLetter() {
            boolean guessed=true;
            Random randomGenerator = new Random();
            char letter = (char) (randomGenerator.nextInt(26)+65);
            chosen=word.hasLetter(letter);


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Random Letter problem please help!

    Took me some time, but I got it

    import java.io.*;
    import java.util.*;
     
    public class RandomLetter {		
     
    public static void main(String args[]) {
     
    Random generator = new Random();
    int RandomNumber = 0;
    int RandomNumberTwo = 0;
    String words[] = new String [3];
    words[0] = "TestForJavaForumsYahh";
    words[1] = "BigTest";
    words[2] = "Forums";
     
    //Generate a random number from 0-2
    RandomNumber = generator.nextInt(3);
     
    //Get the length of the randomly selected string
    int length = words[RandomNumber].length();
     
    //Generate a random number that is from 0-length
    RandomNumberTwo = generator.nextInt(length);
     
    //Print out a character from the random word selected
    System.out.println(words[RandomNumber].charAt(RandomNumberTwo));
     
    System.out.println(words[RandomNumber]);
     
    }	
    }

    This randomly picks a word from your word bank. Then it randomly picks a letter from the word and prints it out.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Incrementing every letter in a string that occupies an odd position.
    By haktheplanet in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2010, 04:45 AM
  3. Sentence and Letter Count Program
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2010, 12:10 AM
  4. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM
  5. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM