Hello DaniWeb, i`m a new member here, so greeting to you.

As the topic title says i`m in bit of a problem, so a little help would be appreciated, I am as newbie as it get`s when talking about JAVA, so here it is.

I`m trying to figuere out a game with 3 players that get random letters from a ALPHABET, and when it finds a word formed in a DICTIONARY it declares that player the winner.

Here is what I have so far:

public class Main {

    static Random noroc = new Random();

    public static void main(String[] args) {
        final char[] ALFABET = {'a', 'b'};
        final String[] DICTIONAR = {"b", "a"};
        int n = ALFABET.length;

        StringBuilder player1 = new StringBuilder();
        StringBuilder player2 = new StringBuilder();
        StringBuilder player3 = new StringBuilder();

        boolean state = false;

        while (state == false) {
            if (state == false) //PLAYER 1
                int r = noroc.nextInt(n);
                char h = ALFABET[r];
                System.out.println("Player 1 got the letters:" + player1 + "\n");


This is just the part for player 1, player 2 would include a similar if. My problem is how do I compare the last x characters in player1 to words in DICTIONAR.

Thanks in advanced, and looking foreward to being a member of this very constructive forum.

7 Years
Discussion Span
Last Post by kennyrgz

You will probably want to use a

HashMap or a HashTable at the very least. If you use a String array, you will need to compare each and every word in the array until you find the word you are looking for.

It may not seem important now, but wait until you load 5k words and your program hangs for a few minutes.

Here is a very simple example:

import java.util.Hashtable;

public class WordSearch {

	private Hashtable<Character, String> dictionary = new Hashtable<Character, String>();
	public void addWord(String word) {
		dictionary.put(word.charAt(0), word);
	public boolean hasWord(String findWord) {
		return dictionary.values().contains(findWord);
	public static void main(String[] args) {
		WordSearch ws = new WordSearch();
		System.out.println(ws.hasWord("Object"));  // true
		System.out.println(ws.hasWord("Java")); // false

However, to answer your question with your implementation, you probably want to do something like this:

public class WordSearch {

    private final String[] DICTIONARY = {"b", "a"};
    private StringBuilder player1 = new StringBuilder();
    private StringBuilder player2 = new StringBuilder();
    private StringBuilder player3 = new StringBuilder();

    public StringBuilder getPlayer(int id) {
    	switch(id) {
    	case 1: return player1;
    	case 2: return player2;
    	case 3: return player3;
    	default: return null;
    public String findWord(StringBuilder player, int numberOfDigits) {
        if (numberOfDigits < player.length()) {
        	for (String word : DICTIONARY)
        		if (player.substring(player.length() - numberOfDigits).equals(word))
        			return word;
        return null;
    public static void main(String[] args) {
    	WordSearch ws = new WordSearch();
    	System.out.println(ws.findWord(ws.getPlayer(1), 2));

Edited by ztini: n/a


Thank you for your help, it was of great help. I surely hope I will get the hand of it someday and be able to implement solutions without so many problems.

Once again thank you for your help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.