CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2010
    Posts
    121

    can someone help me with this if statement

    I am truly stuck with getting the following if statement to work
    Code:
          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?
    Last edited by peahead; March 28th, 2010 at 03:24 AM. Reason: typo - reads better

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: can someone help me with this if statement

    Quote Originally Posted by peahead View Post
    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:
    Code:
     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
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Feb 2010
    Posts
    121

    Re: can someone help me with this if statement

    point taken about naming conventions, which I wasn't aware of.

    In relation to my if statement question, if I remove
    Code:
     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

  4. #4
    Join Date
    Mar 2010
    Posts
    6

    Re: can someone help me with this if statement

    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(""))

  5. #5
    Join Date
    Mar 2010
    Posts
    6

    Re: can someone help me with this if statement

    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(""))

  6. #6
    Join Date
    Feb 2010
    Posts
    121

    Re: can someone help me with this if statement

    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.

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: can someone help me with this if statement

    If you want to only copy the current rack into anagramLetters when it is empty, just check whether it's empty:
    Code:
    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
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  8. #8
    Join Date
    Feb 2010
    Posts
    121

    Re: can someone help me with this if statement

    Solved!! Thanks Dlorde - looks like I couldn't see the woods for the trees :-)

    Final code as follows:
    Code:
            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;
           }
            }
        });

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: can someone help me with this if statement

    Cool

    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
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured