currency - exception

why the following shows IllegalArgumentException>
Locale[] locales = Locale.getAvailableLocales();
Currency c = Currency.getInstance(locales[2])
kitesAsked:
Who is Participating?
 
CEHJCommented:
Since we are so concerned about precision in answering the original question, here is the answer:

"IllegalArgumentException - if the country of the given locale is not a supported ISO 3166 country code."

http://download.oracle.com/javase/6/docs/api/java/util/Currency.html#getInstance(java.util.Locale)
0
 
for_yanCommented:
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.

0
 
for_yanCommented:
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 ?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
for_yanCommented:
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
0
 
kitesAuthor Commented:
how to get euro sign(symbol) based on locale?
0
 
for_yanCommented:

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
0
 
CEHJCommented:
>>how to get euro sign(symbol) based on locale?
String sym = Currency.getInstance(Locale.getDefault()).getSymbol();

Open in new window

0
 
for_yanCommented:
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 : $
0
 
for_yanCommented:
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
0
 
for_yanCommented:
You can probably print it like that:

String euro = "\u20ac";
String pound = "\u00a3";

System.out.println("pound = " + pound);
System.out.println("euro = " + euro);

Open in new window

0
 
for_yanCommented:
Yes, that code above  prints it even for me:
pound = £
euro = €

Open in new window

0
 
CEHJCommented:
>>- 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

0
 
for_yanCommented:
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
0
 
CEHJCommented:
>>
it prints:
locale fr_FR currency EUR symbol : EUR
>>

Sounds like your locales are sketchy
0
 
for_yanCommented:
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.
0
 
kitesAuthor Commented:
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?
0
 
for_yanCommented:
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.
0
 
CEHJCommented:
Try installing language support in the JDK
0
 
kitesAuthor Commented:
thanks...
0
 
for_yanCommented:
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
0
 
kitesAuthor Commented:
I've requested that this question be deleted for the following reason:

question not fully answered.
0
 
for_yanCommented:
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



0
 
for_yanCommented:
See the reason for objection in http:#37001187
0
 
for_yanCommented:


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.
0
 
kitesAuthor Commented:
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!
0
 
for_yanCommented:
Thanks, kites.
Absolutely no problem.
0
 
CEHJCommented:
>>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
0
 
for_yanCommented:

>>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.


0
 
CEHJCommented:
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
0
 
for_yanCommented:
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.
0
 
CEHJCommented:
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 ...
0
 
CEHJCommented:
I fail to see how the accepted solution has any bearing on the actual question...
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.