getISOCountries() method displays only two letters of contry name. Is Get full name of country using this method possible?

徘徊边缘 提交于 2019-12-10 16:19:20

问题


I used following code, but it displays only 2 digit of ISO country name. For example, for "INDIA", it only display "IN". Can I show full name as "INDIA" ?

String[] loc= Locale.getISOCountries();
for(int a=0;a<loc.length;a++)
{
    System.out.println("ISO Contry "+a+":"+loc[a]);
}

I want full name of ISO country. Is it possible to get using this method?


回答1:


Try using getDisplayCountry() method.

Example:

import java.util.Locale;

public class Main {

  public static void main(String [] argv) {

    Locale defaultLocale = Locale.getDefault();
    System.out.println(defaultLocale.getDisplayCountry()); // displays United States
  }
}

EDIT: To get the complete list of full country names, I'm not aware of any methods. Instead what you can do is, download this ISO 3166 Codes which contains the mapping of full country name to 2-letter to two letter ISO 3166 name, use your earlier getISOCountries method to get the 2 letter name and use the mapping.




回答2:


Here we can get onlyfull name of Locale which are available in getAvailableLocale() method.

import java.util.*;

public class Locales
{
  public static void main(String[] args)
  {
    Locale[] locales = Locale.getAvailableLocales();
    for(int i = 0; i < locales.length; i++)
    {
      String locale_name = locales[i].getDisplayName();
      System.out.println((i+1)+":"+locale_name);
    }
  }
}



回答3:


Don´t need to download any file. You can create the Locale object again with the given 2 country letters, so you do it:

String[] locales = Locale.getISOCountries();

    for (String countryCode : locales) {

        Locale obj = new Locale("", countryCode);

        System.out.println("Country Code = " + obj.getCountry()
            + ", Country Name = " + obj.getDisplayCountry());

    }


来源:https://stackoverflow.com/questions/2361168/getisocountries-method-displays-only-two-letters-of-contry-name-is-get-full-n

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!