views:

59

answers:

1

As a programmer of data-entry forms of all kinds, I often find myself making fields for things like Country: <choose from list>, State: <choose from list>, Race/Ethnicity: <choose from list>.

Consider: Perhaps a list the 50 United States names is an easy thing to find (does one include DC?) , but the countries are not. Nearly every site you find has a differing list with all of the political goings on over the years, and they become outdated quickly.

What's the best/common practice regarding population of these kinds of lists? Where does this data come from if it's not given in the specs?

+1  A: 

You can use the ISO-3166 codes for country names. The list for US state names is under ISO 3166-2.

In Java you can build up a country-code list using the Locale object:

    Locale[] locales = Locale.getAvailableLocales();
    for (Locale locale : locales) {
      String iso = locale.getISO3Country();
      String code = locale.getCountry();
      String name = locale.getDisplayCountry();

      if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {
          countryCodeMap.put(name, code);
          countryISOMap.put(name, iso);
      }
    }

Java also uses the ISO-3166 codes (two-letter and three-letter).

Using the ISO lists is much better than rolling your own because the values are standardized.

Vivin Paliath