Home » Java programming language

Java Scanner useLocale() Method with Example

Scanner Class useLocale() method: Here, we are going to learn about the useLocale() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on February 18, 2020

Scanner Class useLocale() method

  • useLocale() method is available in java.util package.
  • useLocale() method is used to use this Scanner locale to the given locale (lo).
  • useLocale() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • useLocale() method does not throw an exception at the time of assigning locale.

Syntax:

    public Scanner useLocale(Locale lo);

Parameter(s):

  • Locale lo – represents the locale to assign this Scanner.

Return value:

The return type of the method is Scanner, it returns an object of "Scanner" type.

Example:

// Java program to demonstrate the example 
// of Scanner useLocale(Locale lo) method of Scanner 

import java.util.*;

public class UseLocaleOfScanner {
    public static void main(String[] args) {
        String str = "Hi, true IncludeHelp! 8 + 2.0f = 10.0f";

        // Instantiate Scanner with the 
        // given str
        Scanner sc = new Scanner(str);

        // Display Scanner
        System.out.println("sc.nextLine(): " + sc.nextLine());

        // By using useLocale() method is to return
        // the locale for this Scanner
        sc.useLocale(Locale.UK);

        // By using locale() method is to print
        // the locale of this Scanner
        System.out.println("sc.locale(): " + sc.locale());

        // close the scanner
        sc.close();
    }
}

Output

sc.nextLine(): Hi, true IncludeHelp! 8 + 2.0f = 10.0f
sc.locale(): en_GB



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.