-4

Suppose I have country name as USA .So I want to convert to USD.I have only country name from webservice.

I have tried this code but how to convert country name into Locale object. Can anybody suggest in this problem?

String countryName=jsonObject.getString("country");
Sushant
  • 254
  • 1
  • 5
  • 15

2 Answers2

3

Provide country code instead of country name

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"));
 }
mayank agrawal
  • 628
  • 5
  • 20
2

You can also use https://github.com/TakahikoKawasaki/nv-i18n library this is specifically designed for this purpose.

From its GitHub page,

Package to support internationalization, containing ISO 3166-1 country code enum, ISO 639-1 language code enum, etc.

Example specific to this question

String countryName = jsonObject.getString("country");
List<CurrencyCode> codes = CurrencyCode.getByCountry(countryName, false)

Simple example to see available list (from Github page),

// List all the currency codes.
for (CurrencyCode code : CurrencyCode.values())
{
    System.out.format("[%s] %03d %s\n", code, code.getNumeric(), code.getName());
}

// List all the country codes.
for (CountryCode code : CountryCode.values())
{
    System.out.format("[%s] %s\n", code, code.getName());
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110