Get Country Name by Country Code using Java

In this tutorial, I am attaching simple program about, How to get Country Name by Country Code using Java.

CountryNameByCode.java

package com.dineshkrish.utilities;
import java.util.Locale;
import java.util.Scanner;
public class CountryNameByCode {
public static String getCountryName(String countryCode) {
String countryName = "";
// Validating the given Input
if(countryCode != null && !countryCode.isEmpty()) {
// Defining the Locale Object using Country Code
Locale locale = new Locale("", countryCode);
// Method to get Country Name
countryName = locale.getDisplayName();
} else {
System.out.println("Invalid Country Code is given !!!");
return "";
}
return countryName;
}
public static void main(String[] args) {
// Defining Scanner Object to read input from user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Country Code : ");
String countryCode = scanner.next();
<a href="https://docs.oracle.com/javase/7/docs/api/" target="_blank">Java API Documentaion</a>	
// Method Invocation
String countryName = getCountryName(countryCode);
System.out.println("Country Name : "+countryName);
// Closing the scanner 
scanner.close();
}
}

Output

—————

Enter the Country Code :
IN
Country Name : India

References

1. Java API Documentaion

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *