Link to home
Start Free TrialLog in
Avatar of kites
kites

asked on

currency - exception

why the following shows IllegalArgumentException>
Locale[] locales = Locale.getAvailableLocales();
Currency c = Currency.getInstance(locales[2])
Avatar of for_yan
for_yan
Flag of United States of America image

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html

IllegalArgumentException - if currencyCode is not a supported ISO 4217 code.

getInstance

public static Currency getInstance(String currencyCode)

    Returns the Currency instance for the given currency code.

    Parameters:
        currencyCode - the ISO 4217 code of the currency
    Returns:
        the Currency instance for the given currency code
    Throws:
        NullPointerException - if currencyCode is null
        IllegalArgumentException - if currencyCode is not a supported ISO 4217 code.

I think locale element 2 for me is "en" - which is probably just English - for language - maybe currency for it is not
defined - is it pound or dollar or australian dollar ?
this works fine for me:

   Locale[] locales = Locale.getAvailableLocales();
        System.out.println(locales.length);
        for(Locale ll : locales){
      
            if(ll.toString().indexOf("US") > -1){
               System.out.println("locale " +  ll +  "currency " +  Currency.getInstance(ll)); 

            }
        }

Open in new window


Output:
152
locale en_UScurrency USD
locale es_UScurrency USD

Open in new window



but locale "en" does not correspond to any particular currency - hence the exception
Avatar of kites
kites

ASKER

how to get euro sign(symbol) based on locale?

I'm not sure how to get sign - that's how it retuirns it:

  Locale[] locales = Locale.getAvailableLocales();
        System.out.println(locales.length);
        for(Locale ll : locales){
        //    System.out.println(ll.toString());
            if(ll.toString().indexOf("FR") > -1){
               System.out.println("locale " +  ll +  " currency " +  Currency.getInstance(ll) + " symbol : " + Currency.getInstance(ll).getSymbol()); 

            }
        }

Open in new window


Output:

locale fr_FR currency EUR symbol : EUR

Open in new window


maybe if I had different language setup on my PC I would have got the symbol  - try if it works for you
but methiod getSymbol() still returns EUR for me
Avatar of CEHJ
>>how to get euro sign(symbol) based on locale?
String sym = Currency.getInstance(Locale.getDefault()).getSymbol();

Open in new window

Yes it returns symbol of dollar for me, but not symbol of euro - I thsink I need
to add language in control panel or somewher to see it
locale en_US currency USD symbol : $
CEHJ, I did that, as you cna see above,  - it still does not return symbol of euro, I guess, if my computer setup does not have that symbol
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, that code above  prints it even for me:
pound = £
euro = €

Open in new window

>>- it still does not return symbol of euro,

So what does it print for this?
sym = Currency.getInstance(Locale.FRANCE).getSymbol();
	System.out.println(sym);

Open in new window

for this:
  System.out.println("locale " +  ll +  " currency " +  Currency.getInstance(ll) + " symbol : " + Currency.getInstance(ll).getSymbol());
it prints:
locale fr_FR currency EUR symbol : EUR
>>
it prints:
locale fr_FR currency EUR symbol : EUR
>>

Sounds like your locales are sketchy
I think if I add language and something in control panel control it will then be showing correct symbol
The question is - will it show it to kites.
Avatar of kites

ASKER

I have tried all the above but no luck . I want the symbol and not EUR. getSymbol() gives "EUR". Will it work for all other currency codes with a generic prgm?
No, it will probably depend  on your where your users will execute it, as you see.
If you have limited number of currencies - better use the symbol explicitly as I showed you.
Try installing language support in the JDK
Avatar of kites

ASKER

thanks...
yes, but if you want to distribute your application, keep in mind that most of your customers will not have this language support instaleed - as both you and me don't have it
Avatar of kites

ASKER

I've requested that this question be deleted for the following reason:

question not fully answered.
I think this question was fully answered

The question:

why the following shows IllegalArgumentException>
Locale[] locales = Locale.getAvailableLocales();
Currency c = Currency.getInstance(locales[2])

the answer is in http:#36996194 and http:#36996228

Then there was additional discussion, whaich  also broght clear results.
We don't like the results - well, that's another story



See the reason for objection in http:#37001187


I think posts http:#36996194 and http:#36996228 together provide the answer, so it should be split between them.

Other posts were illustrations and then dealing with additional questions.
Avatar of kites

ASKER

I am sorry experts. I in no way wanted any loss for you guys, just that I thought I am still unable to print the symbol. Anyways, you guys were tooo good at explaining the question. I certainly do not want to delete the question and please would like to refund the entire points. i was not aware of this thing. THANKS!
Thanks, kites.
Absolutely no problem.
>>I think posts http:#36996194 and http:#36996228 together provide the answer

They don't - in any way i can see.

>>keep in mind that most of your customers will not have this language support instaleed - as both you and me don't have it

They have the support they need installed. If they have a locale in which the currency is the Euro

>>how to get euro sign(symbol) based on locale?

then they will get the Euro symbol.

If YOU want to see the Euro symbol (i suspect you have a locale in which the Euro is NOT the currency) then you
will have to install *additional* language support as i mentioned at http:#36996620

>>I think posts http:#36996194 and http:#36996228 together provide the answer

>They don't - in any way i can see.

CEHJ,

Please, read the original question and let us know if you see any mention of currency symbol there.
Posts  http:#36996194 and http:#36996228 provide response to the original
question, and  post http:#36996620 as well as the most part of http:#37013738, as well
as some of my posts related to currency symbol,
have no relevance for the decision on how this question should be closed, as all
of them deal with additional question which came up after the original question was resolved.


>>keep in mind that most of your customers will not have this language support instaleed - as both you and me don't have it

>They have the support they need installed. If they have a locale in which the currency is the Euro

The author comes from Pacific time zone, so my assumption was
that most of their customers are in the US and most of the computers here, like
my computer and his computer will not have a locale in which the currency is Euro.
It would make sense to take it into account when considering
using the locales in this section of their app.


And, please, try to be a little bit reasonable, and stop your attacks.


It's a situation where the question has migrated since it was first posted.

However neither of your 'answering posts' is relevant, since they relate to the documentation of a method the kites is not even calling
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Come on CEHJ it is all thee same and look below I explained even which locale was a proble because I pasted the code and executed - and the iusseu became absolutely clear after those posts.
CEHJ, please don't do thise attacks, they don't make expoerience better.
I don't know quite why you consider the fact that i'm pointing out that you gave an incorrect answer to the question as an 'attack', but it's instructive that when i do so you then try to shift the focus to later parts of the question - parts of the question which, before i pointed this out, you were eager to say were irrelevant ...
I fail to see how the accepted solution has any bearing on the actual question...
:)