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.
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.
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. :o
Kaustav Banerjee
Re: how do you compare words?
Quote:
Originally Posted by
KaustavBanerjee
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
Re: how do you compare words?
ok here is some of the code:
this is my basic array with 10 words
Code :
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
Code :
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
Re: how do you compare words?
So what is your question now? Where are you stuck?
Re: how do you compare words?
Quote:
Originally Posted by
beginner123
@ 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
Re: how do you compare words?
Re: how do you compare words?
here is my code for the array:
Code :
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:
Code :
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
I just wanted another opinion on the topic. If i solve my problem i will put the solution in both posts
Re: how do you compare words?
Quote:
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.
Re: how do you compare words?
not really sure how to do that.
i tried this in the jTextArea1KeyReleased event:
Code :
if (Words.equals(Words))
{
generateNextWord();
}
but it just changes to the next word after any key from the keyboard is pressed
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.
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.