Solved

Country codes

Posted on 2012-03-28
6
257 Views
Last Modified: 2012-03-28
I've create some constants for country codes, like so:
      public static final String US_COUNTRY_CODE = "US";
      public static final String GB_COUNTRY_CODE = "GB";

but it occurs to me that this may already been built in to Java somewhere.
Is it?
0
Comment
Question by:allelopath
  • 5
6 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 37777615
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37777622
http://docs.oracle.com/javase/6/docs/api/java/util/Locale.html#getISOCountries%28%29

public static String[] getISOCountries()

    Returns a list of all 2-letter country codes defined in ISO 3166. Can be used to create Locales.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37777673
This is how you can print all codes:

        Locale[] los = Locale.getAvailableLocales();
        for(Locale los0: los) {
            System.out.println(los0.getDisplayName() + "   " + los0.getCountry());


        }

Open in new window


Output:


Japanese (Japan)   JP
Spanish (Peru)   PE
English   
Japanese (Japan,JP)   JP
Spanish (Panama)   PA
Serbian (Bosnia and Herzegovina)   BA
Macedonian   
Spanish (Guatemala)   GT
Arabic (United Arab Emirates)   AE
Norwegian (Norway)   NO
Albanian (Albania)   AL
Bulgarian   
Arabic (Iraq)   IQ
Arabic (Yemen)   YE
Hungarian   
Portuguese (Portugal)   PT
Greek (Cyprus)   CY
Arabic (Qatar)   QA
Macedonian (Macedonia)   MK
Swedish   
German (Switzerland)   CH
English (United States)   US
Finnish (Finland)   FI
Icelandic   
Czech   
English (Malta)   MT
Slovenian (Slovenia)   SI
Slovak (Slovakia)   SK
Italian   
Turkish (Turkey)   TR
Chinese   
Thai   
Arabic (Saudi Arabia)   SA
Norwegian   
English (United Kingdom)   GB
Serbian (Serbia and Montenegro)   CS
Lithuanian   
Romanian   
English (New Zealand)   NZ
Norwegian (Norway,Nynorsk)   NO
Lithuanian (Lithuania)   LT
Spanish (Nicaragua)   NI
Dutch   
Irish (Ireland)   IE
French (Belgium)   BE
Spanish (Spain)   ES
Arabic (Lebanon)   LB
Korean   
French (Canada)   CA
Estonian (Estonia)   EE
Arabic (Kuwait)   KW
Serbian (Serbia)   RS
Spanish (United States)   US
Spanish (Mexico)   MX
Arabic (Sudan)   SD
Indonesian (Indonesia)   ID
Russian   
Latvian   
Spanish (Uruguay)   UY
Latvian (Latvia)   LV
Hebrew   
Portuguese (Brazil)   BR
Arabic (Syria)   SY
Croatian   
Estonian   
Spanish (Dominican Republic)   DO
French (Switzerland)   CH
Hindi (India)   IN
Spanish (Venezuela)   VE
Arabic (Bahrain)   BH
English (Philippines)   PH
Arabic (Tunisia)   TN
Finnish   
German (Austria)   AT
Spanish   
Dutch (Netherlands)   NL
Spanish (Ecuador)   EC
Chinese (Taiwan)   TW
Arabic (Jordan)   JO
Belarusian   
Icelandic (Iceland)   IS
Spanish (Colombia)   CO
Spanish (Costa Rica)   CR
Spanish (Chile)   CL
Arabic (Egypt)   EG
English (South Africa)   ZA
Thai (Thailand)   TH
Greek (Greece)   GR
Italian (Italy)   IT
Catalan   
Hungarian (Hungary)   HU
French   
English (Ireland)   IE
Ukrainian (Ukraine)   UA
Polish (Poland)   PL
French (Luxembourg)   LU
Dutch (Belgium)   BE
English (India)   IN
Catalan (Spain)   ES
Arabic (Morocco)   MA
Spanish (Bolivia)   BO
English (Australia)   AU
Serbian   
Chinese (Singapore)   SG
Portuguese   
Ukrainian   
Spanish (El Salvador)   SV
Russian (Russia)   RU
Korean (South Korea)   KR
Vietnamese   
Arabic (Algeria)   DZ
Vietnamese (Vietnam)   VN
Serbian (Montenegro)   ME
Albanian   
Arabic (Libya)   LY
Arabic   
Chinese (China)   CN
Belarusian (Belarus)   BY
Chinese (Hong Kong)   HK
Japanese   
Hebrew (Israel)   IL
Bulgarian (Bulgaria)   BG
Indonesian   
Maltese (Malta)   MT
Spanish (Paraguay)   PY
Slovenian   
French (France)   FR
Czech (Czech Republic)   CZ
Italian (Switzerland)   CH
Romanian (Romania)   RO
Spanish (Puerto Rico)   PR
English (Canada)   CA
German (Germany)   DE
Irish   
German (Luxembourg)   LU
German   
Spanish (Argentina)   AR
Slovak   
Malay (Malaysia)   MY
Croatian (Croatia)   HR
English (Singapore)   SG
Danish   
Maltese   
Polish   
Arabic (Oman)   OM
Turkish   
Thai (Thailand,TH)   TH
Greek   
Malay   
Swedish (Sweden)   SE
Danish (Denmark)   DK
Spanish (Honduras)   HN

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 47

Expert Comment

by:for_yan
ID: 37777693
actually getDisplayCountry() would be better, I guess:

        Locale[] los = Locale.getAvailableLocales();
        for(Locale los0: los) {
            System.out.println(los0.getDisplayCountry() + "   " + los0.getCountry());


        }

Open in new window


Output:

Japan   JP
Peru   PE
   
Japan   JP
Panama   PA
Bosnia and Herzegovina   BA
   
Guatemala   GT
United Arab Emirates   AE
Norway   NO
Albania   AL
   
Iraq   IQ
Yemen   YE
   
Portugal   PT
Cyprus   CY
Qatar   QA
Macedonia   MK
   
Switzerland   CH
United States   US
Finland   FI
   
   
Malta   MT
Slovenia   SI
Slovakia   SK
   
Turkey   TR
   
   
Saudi Arabia   SA
   
United Kingdom   GB
Serbia and Montenegro   CS
   
   
New Zealand   NZ
Norway   NO
Lithuania   LT
Nicaragua   NI
   
Ireland   IE
Belgium   BE
Spain   ES
Lebanon   LB
   
Canada   CA
Estonia   EE
Kuwait   KW
Serbia   RS
United States   US
Mexico   MX
Sudan   SD
Indonesia   ID
   
   
Uruguay   UY
Latvia   LV
   
Brazil   BR
Syria   SY
   
   
Dominican Republic   DO
Switzerland   CH
India   IN
Venezuela   VE
Bahrain   BH
Philippines   PH
Tunisia   TN
   
Austria   AT
   
Netherlands   NL
Ecuador   EC
Taiwan   TW
Jordan   JO
   
Iceland   IS
Colombia   CO
Costa Rica   CR
Chile   CL
Egypt   EG
South Africa   ZA
Thailand   TH
Greece   GR
Italy   IT
   
Hungary   HU
   
Ireland   IE
Ukraine   UA
Poland   PL
Luxembourg   LU
Belgium   BE
India   IN
Spain   ES
Morocco   MA
Bolivia   BO
Australia   AU
   
Singapore   SG
   
   
El Salvador   SV
Russia   RU
South Korea   KR
   
Algeria   DZ
Vietnam   VN
Montenegro   ME
   
Libya   LY
   
China   CN
Belarus   BY
Hong Kong   HK
   
Israel   IL
Bulgaria   BG
   
Malta   MT
Paraguay   PY
   
France   FR
Czech Republic   CZ
Switzerland   CH
Romania   RO
Puerto Rico   PR
Canada   CA
Germany   DE
   
Luxembourg   LU
   
Argentina   AR
   
Malaysia   MY
Croatia   HR
Singapore   SG
   
   
   
Oman   OM
   
Thailand   TH
   
   
Sweden   SE
Denmark   DK
Honduras   HN

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37777728
public class GetCodeForCountry {

    public static String getCode(String country) {
            Locale[] los = Locale.getAvailableLocales();
        for(Locale los0: los) {
            if(country.equals(los0.getDisplayCountry()))return  los0.getCountry();
          //  System.out.println(los0.getDisplayCountry() + "   " + los0.getCountry());


        }

        return null;


    }




    public static void main(String[] args) {


        System.out.println("United States: " + getCode("United States"));
         System.out.println("United Kingdom: " + getCode("United Kingdom"));

}

}

Open in new window



Output:


United States: US
United Kingdom: GB

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37777739
You need to use right names for countries, though.
Say, "Great Britain" return null
Most of the counties are nevertheless straightforward
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now