Click to See Complete Forum and Search --> : can someone help me with this if statement


peahead
March 28th, 2010, 03:20 AM
I am truly stuck with getting the following if statement to work

AnagramButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//if (model.getSize() > 0)
if(AnagramLetters.equals(null)) {
findWordThread = new FindWordThread();
return;
}else
{
AnagramLetters.setText(getCurrentRack().toString());
findWordThread = new FindWordThread();
return;
}
}

Here's what I would like it to do. If a player clicks the anagram button, and only if the AnagramLetters field is left blank, the code should take letters from the players rack to work out anagrams - thankfully this part works!!

However, my problem is, and this is where I am stuck, if a player types letters into the the AnagramLetters field the code should work out anagrams based on these 'typed' letters but the code always overwrites the 'typed' letters with the players rack, which I don't want to happen.

I know this seems fairly simple logic but I cannot get my head around it and I have tried many variations from 'is not null' to 'is null' to 'equals null' with no luck, in fact, it always does the opposite?

Any thoughts?

dlorde
March 28th, 2010, 05:13 AM
However, my problem is, and this is where I am stuck, if a player types letters into the the AnagramLetters field the code should work out anagrams based on these 'typed' letters but the code always overwrites the 'typed' letters with the players rack, which I don't want to happen

If a player types letters into the anagramLetters field, the code executes this line: AnagramLetters.setText(getCurrentRack().toString());This clearly overwrites the typed letters with the current rack. If you don't want this to happen, remove this line. Simples.

Incidentally, the Java naming convention is to use a lowercase letter to start variable and method names and an uppercase letter to start class names. If you don't do this, it makes your code confusing to read and you'll likely get less help. Also, try not to post code with lines commented out.

Remember that you know what your program does and what the rest of the code is like, but people here don't have that context, so posting up an ActionListener full of undescribed fields means a lot of guesswork for anyone trying to help, and will reduce the accuracy, usefulness and number of responses.

Unless in communicating with it [a computer] one says exactly what one means, trouble is bound to result...
A. Turing

peahead
March 28th, 2010, 06:12 AM
point taken about naming conventions, which I wasn't aware of.

In relation to my if statement question, if I remove

AnagramLetters.setText(getCurrentRack().toString());

then the code can't pick up the letters from the current rack if the AnagramLetters field is blank when the code is executed; meaning that the code will produce an anagram of every possible word using all the letters in the alphabet.

That is what I am trying to do with the if statement - if the field is blank when the button is clicked populate it with letters from the current rack otherwise use the 'typed' letters.

Thanks

Battle Programmer Rii
March 28th, 2010, 12:49 PM
Hi,

I believe that instead of checking for :

if(AnagramLetters.equals(""))

or if you still want to check for a null :

if(AnagramLetters.equals(null) || AnagramLetters.equals(""))

Battle Programmer Rii
March 28th, 2010, 12:49 PM
Hi,

I believe you should check for :

if(AnagramLetters.equals(""))

or if you still want to check for a null :

if(AnagramLetters.equals(null) || AnagramLetters.equals(""))

peahead
March 28th, 2010, 02:42 PM
Thanks for help.
Tried your method but same problem still exists. i.e. if the user types letters into AnagramLetters text field the code overwrites them with letters from the players rack. The code then produces anagrams from all the players rack and not the keyed in letters.

Honestly, it's baffling me as it's a simple if statement but it just doesn't work right.

dlorde
March 28th, 2010, 03:07 PM
If you want to only copy the current rack into anagramLetters when it is empty, just check whether it's empty:if (anagramLetters.getText().length() == 0) {
anagramLetters.setText(getCurrentRack().toString());
}Of course, you need to make sure it isn't null first (if that's a possibility).

It would help if you'd explain more clearly exactly what you're trying to achieve.

If we wish to count lines of code, we should not regard them as lines produced but as lines spent...
E. Dijkstra

peahead
March 28th, 2010, 03:50 PM
Solved!! Thanks Dlorde - looks like I couldn't see the woods for the trees :-)

Final code as follows:

public void actionPerformed(ActionEvent e) {
//if (model.getSize() > 0)
if(findWordThread != null)
findWordThread.destroy();
if (AnagramLetters.getText().length() == 0) {
AnagramLetters.setText(getCurrentRack().toString());
findWordThread = new FindWordThread();
return;
}else
{
if(findWordThread != null)
findWordThread.destroy();
findWordThread = new FindWordThread();
return;
}
}
});

dlorde
March 29th, 2010, 04:12 AM
Cool :thumb:

I would strongly recommend that you use curly braces {} with all control statements (if, if..else, loops, etc), even if they are only one-liners. Also, ensure that your indentation is correct - it will save you a lot of grief in the long term.

They know enough who know how to learn...
J. Adams