akrohit
akrohit

Reputation: 1033

How to get Country code/name from Currency code

Given that I have a currency code, how do I get the country code/name in which the currency is applicable? I understand there might be 1:n mapping between currency and country, in that case how to get all the countries for which the currency is applicable?

Upvotes: 4

Views: 3389

Answers (2)

Takahiko Kawasaki
Takahiko Kawasaki

Reputation: 19001

CurrencyCode class in nv-i18n library has getCountryList() method which returns a list of countries that use the currency. Below is a sample command-line application.

import java.util.List;
import com.neovisionaries.i18n.CountryCode;
import com.neovisionaries.i18n.CurrencyCode;

public class CurrencyToCountryList
{
    public static void main(String[] args)
    {
        // For each currency code such as EUR and USD.
        for (String code : args)
        {
            // Get a CurrencyCode instance.
            CurrencyCode currency =
                CurrencyCode.getByCode(code);

            if (currency == null)
            {
                // The code is invalid.
                System.out.format("'%s' is invalid.\n", code);
                continue;
            }

            System.out.format("%s (%s) is used by:\n",
                currency, currency.getName());

            // Get the list of countries using the currency.
            List<CountryCode> countryCodeList =
                currency.getCountryList();

            // For each country.
            for (CountryCode country : countryCodeList)
            {
                // Print the country code and the country name.
                System.out.format("\t%s: %s\n",
                    country, country.getName());
            }
        }
    }
}

If you run this application like this:

$ java -cp nv-i18n-1.15.jar:. CurrencyToCountryList USD

You will get a result shown below:

USD (US Dollar) is used by:
  AS: American Samoa
  BQ: Bonaire, Sint Eustatius and Saba
  EC: Ecuador
  FM: Micronesia, Federated States of
  GU: Guam
  HT: Haiti
  IO: British Indian Ocean Territory
  MH: Marshall Islands
  MP: Northern Mariana Islands
  PA: Panama
  PR: Puerto Rico
  PW: Palau
  SV: El Salvador
  TC: Turks and Caicos Islands
  TL: Timor-Leste
  UM: United States Minor Outlying Islands
  US: United States
  VG: Virgin Islands, British
  VI: Virgin Islands, U.S.

Upvotes: 1

BVdjV
BVdjV

Reputation: 116

In order to get all the countries for the currency, you can run a simple search into the MultiMap object in Java. By running a for to search in the map (say first string is currency and second is country) and an if to check if the currency is the one you look for. If that is true, you can add all the found countries into an Array and then use them further.

Upvotes: 0

Related Questions