Solved

Java string comparison failing

Posted on 2014-01-06
3
284 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 55
login form jsp example 2 53
Why is enum singleton a better approach than static factory 3 52
Html Table Looping (part 2) 5 35
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 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