Question

I have a form that user enter a country name, then I have to convert it to iso2 or iso3 code.

How is it possible? I prefer not to use map, as it seems not running on my Android app.

This code is for converthing iso2 to an actual name, I want the other way around:

Locale l = new Locale("", "CH");
System.out.println(l.getDisplayCountry());
Was it helpful?

Solution

Have you tried Locale.getISO3Country() ?

To actually do the conversion, you might need to loop through all the available locales and look for the one whose getDisplayCountry() matches your input country name.

It doesn't sound efficient (but you said to not use maps), but you might try something like:

Locale convertCountryNameToIsoCode(String countryName)
    for(Locale l : Locale.getAvailableLocales()) {
        if (l.getDisplayCountry().equals(countryName)) {
            return l;
        }
    }
    return null;
}

http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#getISO3Country()

However, if the input comes from a web form, than it would be easier to set the country code directly as the web form input value. If the input comes from an Android native App, then I'm not sure but I'd bet you might a similar thing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top