Solved

Java string comparison failing

Posted on 2014-01-06
3
273 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.  That did the trick.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

11 Experts available now in Live!

Get 1:1 Help Now