Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Comparing enum to a String failing...

Thread view: 
Mikhail Teterin - 15 Nov 2007 03:04 GMT
Hello!

For some reason, I can not properly compare a string to an enum, even when
there is supposed to be a match:

I declare the enum as follows:

private enum Currency {
 USD ("USDAM3L", "US"),
 EUR ("EURAB6E", "DE"),
 GBP ("GBPSB6L", "GB");

 String swapsymbol, ricroot;

 Currency(String _swapsymbol, String _ricroot)
 {
  swapsymbol = _swapsymbol;
  ricroot = _ricroot;
 }
}

I then run through it as:

String swapsymbol = null;
for (Currency currency : Currency.values()) {
 if (bond.currency.equals(currency)) {
  swapsymbol = currency.swapsymbol;
  break;
 } else {
  System.err.println(">" + currency + "< did not match >"
      + bond.currency + "<");
 }
}
if (swapsymbol == null) {
 System.exit(0);
 throw new Exception("Can not find swap-symbol " +
     "for currency " + bond.currency);
}

This results in the following output:

>USD< did not match >EUR<
>EUR< did not match >EUR<
>GBP< did not match >EUR<

As you can see, it failed to notice, that "EUR" equals "EUR" for some
reason. Can anyone, please, help? Thanks!

-mi
Andrew Thompson - 15 Nov 2007 04:22 GMT
Note that an SSCCE* is a lot easier to follow (and test), but..

>As you can see, it failed to notice, that "EUR" equals "EUR"

No.  It seems it failed to notice that a String with value
"EUR" did not equal a Curency object which, when it's
toString() method is called, returns "EUR".

>..for some reason.

That reason being the String "EUR" cannot be equal
to *any* Currency Object, and that is the comparison
being made above.

The 'trick' here is that the String.equals() overrides the
equivalent in Object, thereby it accepts an Object rather
than a String, but you seem to be assuming the equals()
method calls toString() on the Object automatically,
whereas ..it does not do that.

* <http://www.physci.org/codes/sscce.html>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Mikhail Teterin - 15 Nov 2007 22:05 GMT
Many thanks to Andrew, for explaining what's /wrong/. Unfortunately, he ran
out of steam before suggesting, how to do it /right/.

I found a solution for my particular problem in the enum's typeOf() method:

Currency currency = Currency.typeOf(bond.currency);

But I still wish to know, how to compare an enum to a String properly...

-mi
Daniel Pitts - 15 Nov 2007 22:18 GMT
> Many thanks to Andrew, for explaining what's /wrong/. Unfortunately, he ran
> out of steam before suggesting, how to do it /right/.
[quoted text clipped - 6 lines]
>
>  -mi
You don't compare them directly.  That's like asking "how do I compare
an Animal with to word 'Cat'?"

What's your ultimate goal? Do you want to see if the string is the same
as the name of the type?   str.equals(currency.toString()) might work,
or Currency.valueOf(str) == currency.

If at all possible, avoid the String altogether.  I know that's not
always practical, but see if you can figure out how to do that.

Hope this helps,
Daniel.
Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Roedy Green - 15 Nov 2007 23:54 GMT
On Thu, 15 Nov 2007 17:05:55 -0500, Mikhail Teterin
<usenet+mill@aldan.algebra.com> wrote, quoted or indirectly quoted
someone who said :

>But I still wish to know, how to compare an enum to a String properly...

you want to make sure you are using String.equals.  There are equals
methods all over comparing Objects, Currencies etc.

You want one in particular String.equals ( String ).

So use for example Currency.toString(). equals ( somestring );
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Andrew Thompson - 16 Nov 2007 02:50 GMT
>Many thanks to Andrew, for explaining what's /wrong/. Unfortunately, he ran
>out of steam before suggesting, how to do it /right/.

No I still have plenty of plenty of steam.  I 'hid' the answer
in my comments and was wonderring if you had enough nouse
to detect that and develop it further.

The hints were 'automatically' & *toString()*, BTW.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Andrew Thompson - 16 Nov 2007 02:50 GMT
>Many thanks to Andrew, for explaining what's /wrong/. Unfortunately, he ran
>out of steam before suggesting, how to do it /right/.

No I still have plenty of plenty of steam.  I 'hid' the answer
in my comments and was wonderring if you had enough nouse
to detect that and develop it further.

The hints were 'automatically' & *toString()*, BTW.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Roedy Green - 15 Nov 2007 23:00 GMT
On Wed, 14 Nov 2007 22:04:58 -0500, Mikhail Teterin
<usenet+mill@aldan.algebra.com> wrote, quoted or indirectly quoted
someone who said :

>I then run through it as:
>
[quoted text clipped - 6 lines]
>   System.err.println(">" + currency + "< did not match >"
>       + bond.currency + "<");

99% of the time problem is in the code you did NOT post.  If it were
in the code you posted, where you thought the problem was, you would
likely have already found it.

See http://mindprod.com/jgloss/sscce.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.