Is there a better API or library to get all countries

Asked By 0 points N/A Posted on -
qa-featured

Hi,

I have written a java class to get all countries as follows. However, I need an optimized way of getting allcountries from a java library. Please suggest me a open source solution for this.

public class countries{

 

public static String[] WorldCountry=new String[]{"Abkhazia","Afghanistan","Akrotiri and Dhekelia","Åland Islands","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla",

"Antigua and Barbuda","Argentina ","Armenia ","Aruba","Ascension Island",………………….

 

Thanks,

James

SHARE
Answered By 0 points N/A #124173

Is there a better API or library to get all countries

qa-featured

I do not know if there actually exist an API for the list of all countries but if you want to create it your own you can… instead of using a string, like you did above,  i suggest that you use the enum types if you are using Java programming language. For example,

public enum Country {
    FRANCE ("FRA"), GERMANY ("GER"), RUSSIA ("RUS"), USA("USA"), 
    CHINA ("CHN"), JAPAN("JPN"), PHILIPPINES ("PHI"), ... 
    
}

To use the country of enum type,

Public class Test{
	Country country;
	
	public Test(Country country) {
		this.country = country;
	}
	
	public static void main(String[] args) {
		Test cname = new Test(country.PHILIPPINES);
		System.out.println(cname);        
        }
}

OUTPUT : PHI

 

Login/Register to Answer

Related Questions