How to Get a List of Countries in Java


In this example we will show how to display a list of countries in Java.

Source Code

package com.beginner.examples;

import java.util.Locale;

public class ListCountries {

	public static void main(String[] args) {
		
		String[] locales = Locale.getISOCountries();

		for (String countryCode : locales) {

			Locale local = new Locale("", countryCode);

			System.out.println("Country Code = " + local.getCountry() + ", Country Name = " + local.getDisplayCountry());

		}

		System.out.println("Done");
	}

}

Output:

Country Code = AD, Country Name = Andorra
Country Code = AE, Country Name = United Arab Emirates
Country Code = AF, Country Name = Afghanistan
Country Code = AG, Country Name = Antigua and Barbuda
,,,,

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments