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 21 of 21

Thread: word game (problem how to check that the word is in the dictionary)

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default word game (problem how to check that the word is in the dictionary)

    import java.io.*;
    import java.util.Random;
    import java.util.Scanner;
     
    public class WordGame
    {
     
        public static void main(String[] args) throws IOException
        {
            final int RANDOM_LETTERS_COUNT = 10;
            final int TRIALS = 10;
            int score = 0;
            boolean invalidLetter;
     
            char[] letterPool = new char[RANDOM_LETTERS_COUNT];
            String[] userEntries = new String[TRIALS];
     
            // Initialise dictionary.
            System.out.print("\n\nLoading dictionary... ");
            Dictionary dictionary = new Dictionary("brit-a-z.txt");
            System.out.println("done\nDictionary contains " + dictionary.words.length + " words.");
     
            String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int index = (int)Math.floor(Math.random() * 26.0);
     
            String name;
            String word;
     
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            // Read all the words from the dictionary (brit-a-z.txt) into an array
            BufferedReader fr = new BufferedReader(new FileReader("brit-a-z.txt"));
     
            Random r = new Random();
     
     
     
            char[] letters = new char[10];
            int[] points = new int[10];
     
            for(int j = 0; j < 10; j++){
                //System.out.print(alphabet.charAt(r.nextInt(alphabet.length())));
                letters[j] = alphabet.charAt(r.nextInt(alphabet.length()));
                System.out.print("\b");
     
            }
     
            for(int trial = 0; trial < TRIALS; trial++)
            {
                System.out.println("\nTrial " + (trial + 1) + " of " + TRIALS + ": ");
                System.out.println(letters);
                String[] wordsUsed = new String[10];
                System.out.print("Enter word: ");
                name = br.readLine();
                word = name.toUpperCase();
     
                for(int k=trial; k < TRIALS; k++)
                {
                    wordsUsed[k] = word;
                    if (wordsUsed[k].indexOf(word) !=-1){
                        //if the word is present
                        System.out.println(word + " not in dictionary ");
                        trial = k;
                        break;
                    } else if (wordsUsed[k].indexOf(word) ==-1){
                        //word is not in array
                        /*String[] search = readWords("brit-a-z.txt");*/
                        System.out.println(word + " was entered earlier");
                        trial = k;
                        break;
                    }
                    /*letters[k] = 
                    if (letters[k].indexOf(word) !=-1){
                        System.out.println(word + "is an invalid word");
                    }*/
                }
                /*for(int i=0; i<word.length(); i++) 
                {
                    word[i] = word[i].trim();
                }*/
     
                //search array in word
                //if statement - if word is already used, - let user try new word + not increase trials
                //is word is not used then continue
     
                WordGame.validWord(word, letters, score);
     
                points[trial] = word.length();
                score += word.length();
     
            }
            // Report total score at the end of the game.
            System.out.println("\nTotal score: " + score);
        }
     
        // Returns a random letter from the alphabet.
        static char getRandomLetter()
        {
            String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int index = (int)Math.floor(Math.random() * 26.0);
     
            return alphabet.charAt(index);
     
        }
     
        // Checks is a word can be formed from a set of letters.
        static boolean validWord(String word, char[] letters, int score)
        {
            boolean invalidLetter;
     
            char[] copyOfLetters = letters.clone();
     
            for(int i = 0; i < word.length(); i++) {
                invalidLetter = true;
                for(int j = 0; j < copyOfLetters.length; j++) {
                    if(word.charAt(i) == copyOfLetters[j]) {
                        invalidLetter = false;
                        copyOfLetters[j] = '*';
                        break;
                    }
                }
                if(invalidLetter) {
                    return false;
                }
                //number of points
     
            }
            int points = word.length();
            System.out.println(points + " points for " + word);
     
            return true;
     
        }
    }
    Last edited by Rabia; April 28th, 2014 at 09:12 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Can you explain what problems you are having?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Hi
    Thanks for replying me, as you have read this code before the total score of words I have used if statement to check that the word is in the linked dictionary or not and I have to display some statements that the word is repeated or not, it is not in the dictionary. This is the only problem I got here.
    Thank you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Please edit the post and wrap the code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Quote Originally Posted by Norm View Post
    Please edit the post and wrap the code in code tags.
    sorry i don't know how to do it

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    If you can not edit the current post, create a new post.
    See post#2 for what the code tags look like.
    Put the first one before the code
    and the second one after the code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    is that okay

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Is what OK? Look at your post and see how it's displayed.

    You missed this:
    and the second one after the code.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    actually that is comments and if you copy this code to blue j it will be fine but i will try to figure it out what u r saying.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Look at some of the other threads on this forum. 99% of them wrap their code in code tags.
    I'm sure you can figure out how to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Finally

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Is there output from the program that shows the problem?
    Post the output and add some comments to it that says what is wrong and show what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    The dictionary of valid words for the game is given to you in the project folder and it is
    called brit-a-z.txt.

    Here is an example of how your completed program should behave:

    Trial 1 of 10:
    N R A C X F L E N Y
    Enter word: car
    3 points for CAR!

    Trial 2 of 10:
    N R A C X F L E N Y
    Enter word: ran
    3 points for RAN!

    Trial 3 of 10:
    N R A C X F L E N Y
    Enter word: flex
    4 points for FLEX!

    Trial 4 of 10:
    N R A C X F L E N Y
    Enter word: lend
    LEND is an invalid word!

    Trial 5 of 10:
    N R A C X F L E N Y
    Enter word: ran
    RAN was entered earlier

    Trial 5 of 10:
    N R A C X F L E N Y
    Enter word: race
    4 points for RACE!

    Trial 6 of 10:
    N R A C X F L E N Y
    Enter word: fan
    3 points for FAN!

    Trial 7 of 10:
    N R A C X F L E N Y
    Enter word: can
    3 points for CAN!

    Trial 8 of 10:
    N R A C X F L E N Y
    Enter word: fen
    3 points for FEN!

    Trial 9 of 10:
    N R A C X F L E N Y
    Enter word: ann
    ANN not in dictionary.

    Trial 10 of 10:
    N R A C X F L E N Y
    Enter word: near
    4 points for NEAR!

    Total score: 27

    --- Update ---

    the problem is i can't test that the word is in the dictionary or not

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    test that the word is in the dictionary
    Would that be done by the Dictionary class? There isn't a definition for that class posted.

    Does that class have a method that will say if a word is in the dictionary?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    import java.io.*;
     
    public class Dictionary
    {
        public String[] words = null;
     
        public Dictionary(String fileName)
        {
            try
            {
                BufferedReader fh = new BufferedReader(new FileReader(fileName));
     
                int lineCount = 0;
     
                // Count the number of lines in the file.
                while(true)
                {
                    if(fh.readLine() == null) {
                        break;
                    }
                    lineCount++;
                }
     
                fh.close();
     
                words = new String[lineCount];
     
                fh = new BufferedReader(new FileReader(fileName));
     
                for(int i = 0; i < lineCount; i++)
                {
                    words[i] = fh.readLine();
                }
     
                fh.close();
            }
            catch (IOException e)
            {
                System.out.println(e.toString());
            }        
        }
    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Where is the code to search the dictionary for a word?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    that's the problem i got

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    Ok. First Define what the method needs to do and then list the steps the code needs to take to do it.
    When that design is done, work on writing the code to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    i am thinking that create a for loop n in then if statement

    --- Update ---

    i have to submit this in 45 mins but i think i can't complete this

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    i have to submit this in 45 mins but i think i can't complete this
    Probably not.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    Rabia (April 28th, 2014)

  22. #21
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: word game (problem how to check that the word is in the dictionary)

    but thanks for helping

Similar Threads

  1. Replies: 7
    Last Post: March 29th, 2013, 07:38 AM
  2. How to check if a word is there?
    By Idiot_willing_to_learn in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 27th, 2013, 09:00 PM
  3. Spell Check Ms Word style
    By Chander in forum Totally Off Topic
    Replies: 1
    Last Post: December 6th, 2012, 04:11 AM
  4. Replies: 5
    Last Post: August 20th, 2012, 01:01 AM
  5. Unscrambler, trying to get the last word of a dictionary...
    By csharp100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2011, 03:44 AM