i have problem getting the country currency, i am having the country code in database through these code i have to get the country code in java how do i make the code for this please suggest me. i m trying with this example but its not working.
    class Utils {
    public static SortedMap<Currency, Locale> currencyLocaleMap;
    static {
        currencyLocaleMap = new TreeMap<Currency, Locale>(
                new Comparator<Currency>() {
                    public int compare(Currency c1, Currency c2) {
                        return c1.getCurrencyCode().compareTo(
                                c2.getCurrencyCode());
                    }
                });
        for (Locale locale : Locale.getAvailableLocales()) {
            try {
                Currency currency = Currency.getInstance(locale);
                currencyLocaleMap.put(currency, locale);
            } catch (Exception e) {
            }
        }
    }
    public static String getCurrencySymbol(String currencyCode) {
        Currency currency = Currency.getInstance(currencyCode);
        System.out.println(currencyCode + ":-"
                + currency.getSymbol(currencyLocaleMap.get(currency)));
        return currency.getSymbol(currencyLocaleMap.get(currency));
    }
}
  public class GetCurrency {
    public static void main(String[] args) {
        Utils.getCurrencySymbol(Currency.getInstance("INR").getCurrencyCode());
        Utils.getCurrencySymbol(Currency.getInstance(Locale.JAPAN)
                .getCurrencyCode());
        Utils.getCurrencySymbol(Currency.getInstance(Locale.UK)
                .getCurrencyCode());
        Utils.getCurrencySymbol("INR");
    }
}
                The getSymbol() method is used to get the symbol of invoking currency for the default locale.
Try:
//to retrieve currency code
public static String getCurrencyCode(String countryCode) {
    return Currency.getInstance(new Locale("", countryCode)).getCurrencyCode(); 
}
//to retrieve currency symbol 
public static String getCurrencySymbol(String countryCode) {
    return Currency.getInstance(new Locale("", countryCode)).getSymbol(); 
}
                        Debug to determine which currencies are available.
    Set<Currency> avail = Currency.getAvailableCurrencies();
    for (Currency next : avail) {
        System.out.println("----------------------------------------");
        System.out.println("displayName="+next.getDisplayName());
        System.out.println("currencyCode="+next.getCurrencyCode());
        System.out.println("numericCode="+next.getNumericCode());
        System.out.println("symbol="+next.getSymbol());
        System.out.println("toString="+next.toString());
        System.out.println("----------------------------------------");
    }
If any needed currencies are missing/wrong, follow the instructions from the javadoc:
Users can supersede the Java runtime currency data by creating a properties file named
<JAVA_HOME>/lib/currency.properties. The contents of the properties file are key/value pairs of the ISO 3166 country codes and the ISO 4217 currency data respectively. The value part consists of three ISO 4217 values of a currency, i.e., an alphabetic code, a numeric code, and a minor unit. Those three ISO 4217 values are separated by commas. The lines which start with '#'s are considered comment lines. For example,
#Sample currency properties
JP=JPZ,999,0
will supersede the currency data for Japan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With