getUnicodeLocaleType(): This method is available in java.util.Locale class of Java.
Syntax:
String java.util.Locale.getUnicodeLocaleType(String key)
This method takes one argument of type String as its parameter. This method returns the Unicode locale type associated with the specified Unicode locale key for this locale. Returns the empty string for keys that are defined with no type. Returns null if the key is not defined.
Note:
1. Keys are case-insensitive.
2. The key must be two alphanumeric characters ([0-9a-zA-Z]).
Parameters: One parameter is required for this method.
key: the Unicode locale key.
Returns: The Unicode locale type associated with the key, or null if the locale does not define the key.
Throws:
1. IllegalArgumentException - if the key is not well-formed.
2. NullPointerException - if the key is null.
For Example:
Locale locale = new Locale("en", "USA")
String key = "IN"
locale.getUnicodeLocaleType(key) = > It returns null.
Approach 1: When key is well-fromed and key is not null.
Java
import java.util.Locale;public class LocalgetUnicodeLocaleType {public static void main(String[] args) {Locale locale = new Locale("en", "USA");String key = "IN";System.out.println(locale.getUnicodeLocaleType(key));}}
Output:
null
Approach 2: When the key is not well-formed.
Java
import java.util.Locale;public class LocalgetUnicodeLocaleType {public static void main(String[] args) {Locale locale = new Locale("en", "USA");String key = "I";System.out.println(locale.getUnicodeLocaleType(key));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException:Ill-formed Unicode locale key: I at java.base/java.util.Locale.getUnicodeLocaleType(Locale.java:1351)
Approach 3: When the key is null.
Java
import java.util.Locale;public class LocalgetUnicodeLocaleType {public static void main(String[] args) {Locale locale = new Locale("en", "USA");String key = null;System.out.println(locale.getUnicodeLocaleType(key));}}
Output:
Exception in thread "main" java.lang.NullPointerException:Cannot invoke "String.length()" because "s" is null at java.base/java.util.Locale.isUnicodeExtensionKey(Locale.java:2279) at java.base/java.util.Locale.getUnicodeLocaleType(Locale.java:1350)
No comments:
Post a Comment