Skip to content
This repository was archived by the owner on Apr 9, 2019. It is now read-only.

Commit a4f0d14

Browse files
committed
Returns the countries name given country code and vice versa
1 parent e757305 commit a4f0d14

File tree

2 files changed

+335
-0
lines changed

2 files changed

+335
-0
lines changed
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
Afghanistan,AFG
2+
Åland Islands,ALA
3+
Albania,ALB
4+
Algeria,DZA
5+
American Samoa,ASM
6+
Andorra,AND
7+
Angola,AGO
8+
Anguilla,AIA
9+
Antigua and Barbuda,ATG
10+
Argentina,ARG
11+
Armenia,ARM
12+
Aruba,ABW
13+
Australia,AUS
14+
Austria,AUT
15+
Azerbaijan,AZE
16+
Bahamas,BHS
17+
Bahrain,BHR
18+
Bangladesh,BGD
19+
Barbados,BRB
20+
Belarus,BLR
21+
Belgium,BEL
22+
Belize,BLZ
23+
Benin,BEN
24+
Bermuda,BMU
25+
Bhutan,BTN
26+
Bolivia (Plurinational State of),BOL
27+
Bonaire Sint Eustatius and Saba, BES
28+
Bosnia and Herzegovina,BIH
29+
Botswana,BWA
30+
Brazil,BRA
31+
British Virgin Islands,VGB
32+
Brunei Darussalam,BRN
33+
Bulgaria,BGR
34+
Burkina Faso,BFA
35+
Burundi,BDI
36+
Cabo Verde,CPV
37+
Cambodia,KHM
38+
Cameroon,CMR
39+
Canada,CAN
40+
Cayman Islands,CYM
41+
Central African Republic,CAF
42+
Chad,TCD
43+
Chile,CHL
44+
China,CHN
45+
Hong Kong,HKG
46+
Macao,MAC
47+
Colombia,COL
48+
Comoros,COM
49+
Congo,COG
50+
Cook Islands,COK
51+
Costa Rica,CRI
52+
Côte d'Ivoire,CIV
53+
Croatia,HRV
54+
Cuba,CUB
55+
Curaçao,CUW
56+
Czech Republic,CZE
57+
Democratic People's Republic of Korea,PRK
58+
Democratic Republic of the Congo,COD
59+
Denmark,DNK
60+
Djibouti,DJI
61+
Dominica,DMA
62+
Dominican Republic,DOM
63+
Ecuador,ECU
64+
Egypt,EGY
65+
El Salvador,SLV
66+
Equatorial Guinea,GNQ
67+
Eritrea,ERI
68+
Estonia,EST
69+
Ethiopia,ETH
70+
Faeroe Islands,FRO
71+
Falkland Islands (Malvinas),FLK
72+
Fiji,FJI
73+
Finland,FIN
74+
France,FRA
75+
French Guiana,GUF
76+
French Polynesia,PYF
77+
Gabon,GAB
78+
Gambia,GMB
79+
Georgia,GEO
80+
Germany,DEU
81+
Ghana,GHA
82+
Gibraltar,GIB
83+
Greece,GRC
84+
Greenland,GRL
85+
Grenada,GRD
86+
Guadeloupe,GLP
87+
Guam,GUM
88+
Guatemala,GTM
89+
Guernsey,GGY
90+
Guinea,GIN
91+
Guinea-Bissau,GNB
92+
Guyana,GUY
93+
Haiti,HTI
94+
Holy See,VAT
95+
Honduras,HND
96+
Hungary,HUN
97+
Iceland,ISL
98+
India,IND
99+
Indonesia,IDN
100+
Iran (Islamic Republic of),IRN
101+
Iraq,IRQ
102+
Ireland,IRL
103+
Isle of Man,IMN
104+
Israel,ISR
105+
Italy,ITA
106+
Jamaica,JAM
107+
Japan,JPN
108+
Jersey,JEY
109+
Jordan,JOR
110+
Kazakhstan,KAZ
111+
Kenya,KEN
112+
Kiribati,KIR
113+
Kuwait,KWT
114+
Kyrgyzstan,KGZ
115+
Lao People's Democratic Republic,LAO
116+
Latvia,LVA
117+
Lebanon,LBN
118+
Lesotho,LSO
119+
Liberia,LBR
120+
Libya,LBY
121+
Liechtenstein,LIE
122+
Lithuania,LTU
123+
Luxembourg,LUX
124+
Madagascar,MDG
125+
Malawi,MWI
126+
Malaysia,MYS
127+
Maldives,MDV
128+
Mali,MLI
129+
Malta,MLT
130+
Marshall Islands,MHL
131+
Martinique,MTQ
132+
Mauritania,MRT
133+
Mauritius,MUS
134+
Mayotte,MYT
135+
Mexico,MEX
136+
Micronesia (Federated States of),FSM
137+
Monaco,MCO
138+
Mongolia,MNG
139+
Montenegro,MNE
140+
Montserrat,MSR
141+
Morocco,MAR
142+
Mozambique,MOZ
143+
Myanmar,MMR
144+
Namibia,NAM
145+
Nauru,NRU
146+
Nepal,NPL
147+
Netherlands,NLD
148+
New Caledonia,NCL
149+
New Zealand,NZL
150+
Nicaragua,NIC
151+
Niger,NER
152+
Nigeria,NGA
153+
Niue,NIU
154+
Norfolk Island,NFK
155+
Northern Mariana Islands,MNP
156+
Norway,NOR
157+
Oman,OMN
158+
Pakistan,PAK
159+
Palau,PLW
160+
Panama,PAN
161+
Papua New Guinea,PNG
162+
Paraguay,PRY
163+
Peru,PER
164+
Philippines,PHL
165+
Pitcairn,PCN
166+
Poland,POL
167+
Portugal,PRT
168+
Puerto Rico,PRI
169+
Qatar,QAT
170+
Republic of Korea,KOR
171+
Republic of Moldova,MDA
172+
Réunion,REU
173+
Romania,ROU
174+
Russian Federation,RUS
175+
Rwanda,RWA
176+
Saint Barthélemy,BLM
177+
Saint Helena,SHN
178+
Saint Kitts and Nevis,KNA
179+
Saint Lucia,LCA
180+
Saint Martin (French part),MAF
181+
Saint Pierre and Miquelon,SPM
182+
Saint Vincent and the Grenadines,VCT
183+
Samoa,WSM
184+
San Marino,SMR
185+
Sao Tome and Principe,STP
186+
Saudi Arabia,SAU
187+
Senegal,SEN
188+
Serbia,SRB
189+
Seychelles,SYC
190+
Sierra Leone,SLE
191+
Singapore,SGP
192+
Sint Maarten (Dutch part),SXM
193+
Slovakia,SVK
194+
Slovenia,SVN
195+
Solomon Islands,SLB
196+
Somalia,SOM
197+
South Africa,ZAF
198+
South Sudan,SSD
199+
Spain,ESP
200+
Sri Lanka,LKA
201+
State of Palestine,PSE
202+
Sudan,SDN
203+
Suriname,SUR
204+
Svalbard and Jan Mayen Islands,SJM
205+
Swaziland,SWZ
206+
Sweden,SWE
207+
Switzerland,CHE
208+
Syrian Arab Republic,SYR
209+
Tajikistan,TJK
210+
Thailand,THA
211+
The former Yugoslav Republic of Macedonia,MKD
212+
Timor-Leste,TLS
213+
Togo,TGO
214+
Tokelau,TKL
215+
Tonga,TON
216+
Trinidad and Tobago,TTO
217+
Tunisia,TUN
218+
Turkey,TUR
219+
Turkmenistan,TKM
220+
Turks and Caicos Islands,TCA
221+
Tuvalu,TUV
222+
Uganda,UGA
223+
Ukraine,UKR
224+
United Arab Emirates,ARE
225+
United Kingdom of Great Britain and Northern Ireland,GBR
226+
United Republic of Tanzania,TZA
227+
United States of America,USA
228+
United States Virgin Islands,VIR
229+
Uruguay,URY
230+
Uzbekistan,UZB
231+
Vanuatu,VUT
232+
Venezuela (Bolivarian Republic of),VEN
233+
Viet Nam,VNM
234+
Wallis and Futuna Islands,WLF
235+
Western Sahara,ESH
236+
Yemen,YEM
237+
Zambia,ZMB
238+
Zimbabwe,ZWE

GoogleAndriodPrep_1/ISO_HashMap.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* @author Remel Kabir
3+
*
4+
* This is preparation practice program for google's Applied CS with Android.
5+
*
6+
* This program takes in a three-letter country code and returns the full name of the country to which it belongs. For example:
7+
8+
Input | Output
9+
----- | ----------------------------------------------------
10+
GBR | United Kingdom of Great Britain and Northern Ireland
11+
IDN | Indonesia
12+
IND | India
13+
As an extension, if the input is greater than 3 letters, it is consider as the name of a country,
14+
and returns the three-letter code for it. Write a helpful error message if the input is neither a valid code nor a country name.
15+
*/
16+
import java.io.*;
17+
import java.util.*;
18+
19+
public class ISO_HashMap {
20+
private static HashMap<String, String> codeName = new HashMap<String, String>(); //creating a HashMap to put the name and code of the country.
21+
public static void main(String args[]){
22+
23+
String fileName= "CodeAndNameOfCountry.txt"; //initailzing the filename to the txt file that contains all the code and name.
24+
String line = null; // variable to read one line at a time from the txt file.
25+
26+
try{
27+
28+
FileReader fileReader = new FileReader(fileName); //reads the .txt file
29+
BufferedReader bufferedReader = new BufferedReader(fileReader); // this object will import method that reads the .txt file line by line.
30+
31+
while((line=bufferedReader.readLine()) != null ){ // keeps reading till every line has been processed.
32+
//Temporary array and variable to process all the codes and names.
33+
String[] tempCodeName = line.split(","); // splits the string if there is a comma in the line.
34+
String CountryName = tempCodeName[0];// first String of the line is the name of the country
35+
String CountryCode = tempCodeName[1];// second String of the line is the Code of the Country
36+
//HashMap Entry adding.
37+
codeName.put(CountryCode, CountryName); //adds the name and the code of the country in the HashMap, Code as the key and Name as the value;
38+
}//end of while loop
39+
40+
bufferedReader.close();// Closing the file after reading it.
41+
}//end of try
42+
43+
catch(FileNotFoundException ex) {
44+
System.out.println("File Not Found '" + fileName + "'");
45+
}//end of catch
46+
47+
catch(IOException ex) {
48+
System.out.println("Error '" + fileName + "'");
49+
}//end of catch
50+
51+
inputOutput();
52+
53+
}//end of main
54+
55+
private static void inputOutput() {
56+
Scanner scan = new Scanner(System.in);// scan object for scanning.
57+
58+
String inputForInfo;// user's input
59+
60+
System.out.println("Please Enter a three Digit Country Code to get the full name of the country:");
61+
inputForInfo= scan.nextLine();//read the line that user inputed and saves it in the variable InputForInfo
62+
int lengthOfInput = inputForInfo.length(); // length of the input to check if the user input anything other than a three digit country code.
63+
if(!validInput(inputForInfo)){
64+
System.out.println("Not a valid Input. It's neither a code of a country nor it's name ");
65+
}//if
66+
if(lengthOfInput == 3){
67+
System.out.println(getCountry(inputForInfo));
68+
}//if
69+
else
70+
System.out.println(getCode(inputForInfo));
71+
}//end of inputOutput
72+
73+
//To check if the input is either a valid code or country's name
74+
private static boolean validInput(String input){
75+
//returns true if the the three digits code is either a name or code of a country.
76+
if(codeName.containsValue(input)) return true;
77+
if(codeName.containsKey(input)) return true;
78+
return false; // else returns false.
79+
}//end of validInput
80+
81+
// returns the code if country's name is entered.
82+
private static String getCode(String input){
83+
for (Map.Entry<String, String> en : codeName.entrySet()) {
84+
if (en.getValue().equals(input)) {
85+
return en.getKey();
86+
}//if
87+
}//end of for
88+
return "Code of the country not found";
89+
}//end of getCode
90+
91+
//returns the name of the country if correct code entered
92+
private static String getCountry(String input){
93+
if(!codeName.containsKey(input)) return "country not Found";
94+
return codeName.get(input);
95+
}
96+
97+
}//end of class

0 commit comments

Comments
 (0)