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

Thread: how do you compare words?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do you compare words?

    I am trying to make a typing game where random words come on the screen and the user has to type the word out in the textarea.
    I created an array with 10 words which a displayed using a label, theres a start button and a textarea for the user to type in.

    When i click start and a word appears i want to be able to enter that word into the textarea and compare it
    and when the user clicks the space bar it moves onto the next word.

    I would like to maybe count any errors made/add a timer but I am not focusing on that part right now.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how do you compare words?

    Welcome beginner 123.

    I suggest you find a starting point and work on one part at a time. Without seeing what you have so far it is difficult to see where you got stuck. Posting your code with your question will make it easier to offer advice.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Kolkata, India
    Posts
    8
    My Mood
    Cool
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: how do you compare words?

    If you are using String and not character arrays, you can use the Java built in function strcmp or even a conditional statement with the == operator.
    Just get the input with Buffered reader class from the textfield and match it with your respective array element.

    I guess that will do the job.

    Kaustav Banerjee
    Last edited by copeg; November 15th, 2012 at 03:49 PM. Reason: Removed link

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how do you compare words?

    Quote Originally Posted by KaustavBanerjee View Post
    If you are using String and not character arrays, you can use the Java built in function strcmp or even a conditional statement with the == operator.
    No, and no. Java does not have a 'built in function strcmp', and == compares objects, not the object value (calling String.equals() method on the instance compares the value). Given this is your first post, giving misleading advice at best, the link you posted has been removed

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do you compare words?

    ok here is some of the code:

    this is my basic array with 10 words
     public int randomNumber;
        public String[] Words = new String[10];
        public int WordCounter=0;
     
     
        private void fillLists() 
        { 
            Words[0] = "great";
            Words[1] = "different";
            Words[2] = "point";
            Words[3] = "mountain";
            Words[4] = "home";
            Words[5] = "jumped";
            Words[6] = "picture";
            Words[7] = "mobile";
            Words[8] = "fox";
            Words[9] = "dog";
     
     
        }

    and this is just the code to display the words
    private void generateNextWord() {
            WordCounter++;
            updateFields();
        }
     
        private void updateFields() {
     
            randomNumber = r.nextInt(10);
            wordfield.setText(Words[randomNumber]);
    }
    @ KaustavBanerjee - I only just started learning java so that way sounds a bit complicated

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how do you compare words?

    So what is your question now? Where are you stuck?

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how do you compare words?

    Quote Originally Posted by beginner123 View Post
    @ KaustavBanerjee - I only just started learning java so that way sounds a bit complicated
    Not just complicated, incorrect (see my post above, which somehow was invisible for a short time).

    If you wish to compare a String to another, use the equals() method. If you wish to check if a work is within a collection of words, use a Set

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how do you compare words?

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/65074-typing-application.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do you compare words?

    here is my code for the array:
    Random r = new Random();
     
     
        public int randomNumber;
        public String[] Words = new String[10];
        public int WordCounter=0;
        public int CorrectCounter =0;
     
        private void fillLists() 
        { 
            Words[0] = "great";
            Words[1] = "different";
            Words[2] = "point";
            Words[3] = "mountain";
            Words[4] = "home";
            Words[5] = "jumped";
            Words[6] = "picture";
            Words[7] = "mobile";
            Words[8] = "fox";
            Words[9] = "dog";
     
     
        }

    and here is the code to display the words:
    private void generateNextWord() {
            WordCounter++;
            updateFields();
        }
     
        private void updateFields() {
     
            randomNumber = r.nextInt(10);
            wordfield.setText(Words[randomNumber]);
    }

    my problem is how to compare the words in the array with the words the user enters in the textarea?

    --- Update ---

    Quote Originally Posted by copeg View Post
    This thread has been cross posted here:

    http://www.java-forums.org/new-java/65074-typing-application.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    I just wanted another opinion on the topic. If i solve my problem i will put the solution in both posts

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how do you compare words?

    my problem is how to compare the words in the array with the words the user enters in the textarea?
    Get the word from the textarea and iterate over the array comparing the textarea word to the array word using the equals method.

  11. #11
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do you compare words?

    not really sure how to do that.
    i tried this in the jTextArea1KeyReleased event:
    if (Words.equals(Words))
            {
                generateNextWord();
     
            }

    but it just changes to the next word after any key from the keyboard is pressed

  12. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how do you compare words?

    if (Words.equals(Words)) ..When is this true and when is it false?

    jTextArea1KeyReleased event ..Are you sure this is the best time to check? KeyReleased is something that happens any time a key is let up. I would think you should check after they type everything and press enter.

    but it just changes to the next word ..I am not sure what you mean by "it" but since you said it happens every time a key is pressed perhaps asking yourself the above questions first will solve this also.

    --- Update ---

    if (Words.equals(Words)) ..When is this true and when is it false?

    jTextArea1KeyReleased event ..Are you sure this is the best time to check? KeyReleased is something that happens any time a key is let up. I would think you should check after they type everything and press enter.

    but it just changes to the next word ..I am not sure what you mean by "it" but since you said it happens every time a key is pressed perhaps asking yourself the above questions first will solve this also.

  13. #13
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do you compare words?

    yes i know using if (Words.equals(Words)) is wrong. I know i need some kind of loop.

    I thought maybe i would use the jTextArea1KeyReleased event so when the last letter of the word being typed is released, it would check if the word from the array is correct. But i think checking the word after pressing enter would be better.

Similar Threads

  1. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  2. How do I compare between the array of char and array of words !!?
    By bady2050 in forum Collections and Generics
    Replies: 1
    Last Post: May 5th, 2012, 05:36 PM
  3. image compare
    By ana0429 in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2012, 03:03 AM
  4. Compare method
    By Evelina in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 16th, 2010, 02:07 PM
  5. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM