Solved

Country codes

Posted on 2012-03-28
6
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

630 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