ISO2 country code from country name

2019-01-23 01:09发布

Is there an easy way in Java to get the ISO2 code from a given country name, for example "CH" when given "Switzerland"?

The only solution I can think of at the moment is to save all the country codes and names in an array and iterate over it. Any other (easier) solutions?

9条回答
Explosion°爆炸
2楼-- · 2019-01-23 01:55

This is definitely the answer you are looking for, it is late but hopefully it will hepl someone

String[] isoCountries = Locale.getISOCountries();
    for (String country : isoCountries) {
        Locale locale = new Locale("en", country);
        String iso = locale.getISO3Country();
        String code = locale.getCountry();
        String name = locale.getDisplayCountry();
        System.out.println(iso + " " + code + " " + name);

    }

I have added the print statement so that you can get a good look at the result. You can substitute "CH" for that country variable in the third line and it will give you "Afghanistan".

查看更多
在下西门庆
3楼-- · 2019-01-23 01:56

Nope, you've got the easy solution already: The array (or DB table, or a keyed structure like a hash map) solution is pretty much the standard way of doing this. I guess there may also be some web services available that can provide the list, but I think that would be overkill (its not worth relying on a third party system to be working for something this simple). Similar arrays would be used for lists of counties/provinces/states within a country if you needed that level of detail.

The only thing to beware of with that is if you're accepting the country name as user input, you'll need to watch for spelling errors, incorrect capitalisation, and using alternative names for countries (eg "Switzerland" could legitimately be referred to as "Schweiz" or "Suisse" or "The Swiss Confederation", or a number of other variants.

More commonly, user input is restricted to known country codes by providing a drop-list so that the user picks the country he wants and the program knows immediately that it is "CH". This also requires the same array to be in place, but is referenced in the other direction, and is more reliable (since there is only one possible code for each country).

Also note that although the list of countries in the world doesn't change much, it does change. You should make sure you keep it up-to-date.

查看更多
Melony?
4楼-- · 2019-01-23 02:00

You could use the built-in Locale class:

Locale l = new Locale("", "CH");
System.out.println(l.getDisplayCountry());

prints "Switzerland" for example. Note that I have not provided a language.

So what you can do for the reverse lookup is build a map from the available countries:

public static void main(String[] args) throws InterruptedException {
    Map<String, String> countries = new HashMap<>();
    for (String iso : Locale.getISOCountries()) {
        Locale l = new Locale("", iso);
        countries.put(l.getDisplayCountry(), iso);
    }

    System.out.println(countries.get("Switzerland"));
    System.out.println(countries.get("Andorra"));
    System.out.println(countries.get("Japan"));
}
查看更多
登录 后发表回答