Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: If statement checking if a string value is equal to 2 enums

  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default If statement checking if a string value is equal to 2 enums

    Greetings,
    On line 6 I am trying to check if the string value of anim.getName() == the name of actionDelta + direction. The println prints "walkingnorth' which is correct, but for some reason it won't work in the IF statement. It will work if I change it to if (anim.getName() == "walkingnorth";
    Thanks :)

    Java Code:
        public enum ActionEnum { standing, walking };
        private ActionEnum action;
        private ActionEnum actionDelta;
        public enum DirectionEnum { north, south, east, west };
        private DirectionEnum direction;
    Java Code:
                                if (((Mobile)o).getAction() != ((Mobile)o).getActionDelta()){
                                    Iterator<Animation> animit = o.getAnimsList().iterator();
                                    System.out.println(((Mobile)o).getActionDelta().name() + ((Mobile)o).getDirection().name()); // prints "walkingnorth"
                                    while(animit.hasNext()){
                                        Animation anim = animit.next();
                                        if (anim.getName() == ((Mobile)o).getActionDelta().toString() + ((Mobile)o).getDirection().toString()){
                                            System.out.println("ANIMATION FOUND");

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: If statement checking if a string value is equal to 2 enums

    Hi,
    try printing the right side of the == operator in line 6 and see what happens :P

    BR,
    Kalin
    "One of the greatest ways to learn something is trying to teach it to someone else."

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: If statement checking if a string value is equal to 2 enums

    Ah, the old "comparing strings with ==". Must be the one trap that everyone new to Java falls into.

    String is an object, use its equals() method to compare it to another String object.

  4. #4
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: If statement checking if a string value is equal to 2 enums

    I did that on line 3 and it prints out "walkingnorth" which is correct :) (it's the same whether i use .name() or .toString())
    I don't understand why I can't use it in the IF statement though :(

  5. #5
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: If statement checking if a string value is equal to 2 enums

    Quote Originally Posted by gimbal2 View Post
    Ah, the old "comparing strings with ==". Must be the one trap that everyone new to Java falls into.

    String is an object, use its equals() method to compare it to another String object.
    Thank you so much ! It worked :D

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: If statement checking if a string value is equal to 2 enums

    That's because the two objects are not equal. It has nothing to do with the Strings being equal. Think of it this way. Assume you have a bucket and a box which are objects. Both contain items. When you use == you compare the bucket to the box. When you use equals, you compare the items. Now consider two Strings. Each string object could be different but their actual content (the characters that make up the string) could be identical.

    Regards,
    Jim
    Last edited by jim829; 07-05-2013 at 05:13 PM.
    Teedo likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2012, 11:31 PM
  2. String not equal to...... :(
    By GrumpyBum in forum New To Java
    Replies: 5
    Last Post: 04-21-2012, 10:41 PM
  3. checking array items are equal
    By trishtren in forum New To Java
    Replies: 5
    Last Post: 10-16-2011, 12:23 PM
  4. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 06:21 AM
  5. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 0
    Last Post: 11-18-2007, 02:13 AM

Posting Permissions

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