Solved

Java string comparison failing

Posted on 2014-01-06
3
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

691 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