Solved

Java string comparison failing

Posted on 2014-01-06
3
276 Views
Last Modified: 2014-01-06
I have a java method that passes in a string and is comparing the value in the string to a direction (i.e "East", "West", "North", "South").  When it finds a match it returns an Directions object (i.e Directions.East, Directions.West, Directions.North, Directions.South).  I am making the call to the method as follows: convertDirection(eElement.getAttribute("startDirection"));

Here is the method:
   private static Directions.Direction convertDirection(String dir)
   {
      System.out.printf("Direction in method: %s Length=%d\n", dir, dir.length());
      if(dir=="East")
      {
         return Directions.East;
      }
      if(dir=="West")
      {
         return Directions.West;
      }
      if(dir=="North")
      {
         return Directions.North;
      }
      if(dir=="South")
      {
         return Directions.South;
      }
      return Directions.North;
   } 

Open in new window


If the XML attribute contains "West", when I debug the method, dir shows "West" and the string length of dir is 4 so I don't have any extra unseen characters.  However, the if(dir=="West") statement returns false so it doesn't return my West direction and falls through to the end of the method and returns North.  If I hard code the call as follows:
convertDirection("West");

The method works correctly.  I've tried creating a string variable and assigning the results of eElement.getAttribute("startDirection") to the variable and passing that into the method but it still doesn't work.

Any help is greatly appreciated as I have to have this working by tomorrow morning and I am at a complete loss.
0
Comment
Question by:dyarosh
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 39760944
Try using dir.equals("West") instead of the == operator. The equals() method will compare the strings' contents. The == operator is checking to see if the two strings are the exact same object, which will usually fail.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39760948
Also, if Directions is an enum, you could use Directions.valueOf(dir) and it'll do the same thing in one line as the whole method you wrote. Be careful, though, because that method is case-sensitive and throws an IllegalArgumentException if the value doesn't exist.
0
 

Author Closing Comment

by:dyarosh
ID: 39761168
Thank you.  That did the trick.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now