Locale getDisplayCountry() inLocale in Java

getDisplayCountry(): This method is available in java.util.Locale class of Java.

Syntax:

String java.util.Locale.getDisplayCountry(Locale inLocale)

This method takes one argument of type Locale as its parameter. This method returns a name for the locale's country that is appropriate for display to the user. If possible, the name returned will be localized according to inLocale.

Parameters: One parameter is required for this method.

inLocale: The locale for which to retrieve the displayed country.

Returns: The name of the country appropriate to the given locale.

Throws: NullPointerException - if inLocale is null

For Example:

Locale locale = new Locale("en", "US")

Locale inLocale = new Locale("fr", "FR")

locale.getDisplayCountry(inLocale) = > It returns États-Unis.

locale1.getDisplayCountry(locale) = > It returns France.

Approach 1: When inLocale is not null.

Java

import java.util.Locale;

public class LocalegetDisplayCountry2 {
    public static void main(String[] args) {

        Locale locale = new Locale("en""US");

        Locale inLocale = new Locale("fr""FR");
        System.out.println(locale.getDisplayCountry(inLocale));

        System.out.println(inLocale.getDisplayCountry(locale));
    }
}

Output:

États-Unis

France


Approach 2: When inLocale is null.

Java

import java.util.Locale;

public class LocalegetDisplayCountry2 {
    public static void main(String[] args) {

        Locale locale = new Locale("en""US");

        Locale inLocale = null;
        System.out.println(locale.getDisplayCountry(inLocale));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.Locale.getDisplayString(Locale.java:1917) at java.base/java.util.Locale.getDisplayCountry(Locale.java:1913)


No comments:

Post a Comment