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
Solved

Java string comparison failing

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 49
JAVA API design with micro service cloud in mind 1 47
collection output issue 9 37
web project error add remove 1 29
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This video teaches viewers about errors in exception handling.

838 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