Comparing enum to a String failing...

M

Mikhail Teterin

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
 
A

Andrew Thompson

Mikhail Teterin wrote:

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>

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 
M

Mikhail Teterin

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
 
D

Daniel Pitts

Mikhail said:
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
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.
 
R

Roedy Green

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 + "<");

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
 
R

Roedy Green

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 );
 
A

Andrew Thompson

Mikhail said:
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 
A

Andrew Thompson

Mikhail said:
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top