Search code examples
javastringenumscase-sensitive

Java Enum.name() unexpected result containing "?"


Today, one of my users has had my app crash on his device because of a strange error. This is the resulting message in my backend:

util.java.MissingResourceException: (Can't find bundle key med?um)

The bundle key is supposed to be "medium" and is build with the following (pseudo) code:

STAGE stage = something.getStage();
String stageValue = stage.name().toLowerCase();

where STAGE is:

public enum STAGE
{
    EASY,
    MEDIUM,
    HARD;
}

I was under the impression that the name() function would always return the value of the enum as it is in the code: "MEDIUM". How is it possible that it contains a questionmark?


Solution

  • You need to be carefull about how you treat lower and upper case in java...

    in places like Turkey where the alphabet has kind of similar vowels you can find

    I and İ

    see table below

    enter image description here

    so in that case you need to explicit use a Locale so java can consider some language especific convertions...

    MEDIUM is read as a turkey word will have a lower case like medıum (note the vowel has no upper dot), and this will make the app crash since you dont have such a value declared in the enumerator...