Skip to content
Related Articles

Related Articles

Improve Article

Locale.Builder setScript() method in Java with Examples

  • Last Updated : 30 Jul, 2019
Geek Week

The setScript(String) method of java.util.Locale.Builder class in Java is used to set this Locale.Builder to the specified script. It means that this method will set the current script of Locale.Builder instance to match the provided script and return it. If the specified script is null or empty, then the script of this LocaleBuilder is removed. A well-formed script value comprises og a 4-letter script code as defined by the ISO 15924 standard.

Syntax:

public Locale.Builder setScript(String script)

Parameter: This method accepts the script as a parameter which is the String that is to be set to this Locale.Builder instance.

Return Value: This method returns an Locale.Builder instance with the script set of this Locale.Builder to the specified script.

Exception: This method throws following Exceptions:



  • IllformedLocaleException: if the specified script has any ill formed fields

Program 1:




// Java program to demonstrate
// the above method
  
import java.util.*;
import java.util.Locale.*;
  
public class LocaleBuilderDemo {
    public static void main(String[] args)
    {
  
        // Creating a new Locale.Builder
        Locale.Builder localeBuilder
            = new Builder();
  
        // Displaying Locale.Builder
        System.out.println("LocaleBuilder: "
                           + localeBuilder);
  
        // setting the script of Locale.Builder
        String script = "Gujr";
        System.out.println("Setting the script: "
                           + script);
  
        localeBuilder
            = localeBuilder.setScript(script);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
    }
}
Output:
LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the script: Gujr
Updated LocaleBuilder: java.util.Locale$Builder@232204a1

Program 2:




// Java program to demonstrate
// the above method
  
import java.util.*;
import java.util.Locale.*;
  
public class LocaleBuilderDemo {
    public static void main(String[] args)
    {
  
        // Creating a new Locale.Builder
        Locale.Builder localeBuilder
            = new Builder();
  
        // Displaying Locale.Builder
        System.out.println("LocaleBuilder: "
                           + localeBuilder);
  
        // setting the script of Locale.Builder
        String script = "asda@vasdev#";
        System.out.println("Setting the script: "
                           + script);
          
        try{
        localeBuilder
            = localeBuilder.setScript(script);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
                           }
        catch(Exception e)
        {
            System.out.println(e);
            }
    }
}
Output:
LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the script: asda@vasdev#
java.util.IllformedLocaleException:
 Ill-formed script: asda@vasdev# [at index 0]

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Locale.Builder.html#setScript-java.lang.String-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :