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.
Code :
private void chooseLetter() {
boolean guessed=true;
Random randomGenerator = new Random();
char letter = (char) (randomGenerator.nextInt(26)+65);
chosen=word.hasLetter(letter);
Re: Random Letter problem please help!
Took me some time, but I got it :)
Code :
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.