Java Scanner locale() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Scanner

Definition

In Java, sc.locale() returns the java.util.Locale that controls how the scanner parses decimal points, grouping separators, and localized number strings.

Every newly constructed scanner starts with the JVM's default format locale (from Locale.getDefault(Locale.Category.FORMAT)) until changed by useLocale().

An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.locale(...).

Syntax

Check which Locale is active on a scanner using sc.locale().

Java
Scanner sc = new Scanner("3.5").useLocale(Locale.US);
Locale active = sc.locale();          // en_US
String lang = active.getLanguage();   // "en"
CallWhat it does
sc.locale()Returns the active Locale object.
sc.locale().toString()Returns the locale tag string (such as en_US or de_DE).
Locale saved = sc.locale();Captures the starting locale before a temporary change.

Because different computers have different default locales, avoid assuming what new Scanner(...).locale() prints without calling useLocale(...) first.

Library declarations
Java
// java.util.Scanner
public Locale locale()

Parameters

ParameterDescription
Nonelocale() takes no parameters.

Return Value

Return ValueDescription
LocaleThe scanner's current Locale.

Compare with Similar Methods

Locale inspection alongside configuration:

MethodHow it differs
useLocale(loc)Updates the locale returned by locale().
reset()Reverts locale() back to the JVM default format locale.
radix()Inspects the integer base instead of regional rules.

Java Examples

Example 1 — Inspect Active Locale Codes After Calling useLocale()

Set three well-known locales in sequence and print their language, country, and string representations.

Java
import java.util.Locale;
import java.util.Scanner;

public class ScannerLocaleTags {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("1.0")) {
            sc.useLocale(Locale.US);
            System.out.println("US:      " + sc.locale() + " (lang=" + sc.locale().getLanguage() + ")");
            sc.useLocale(Locale.GERMANY);
            System.out.println("Germany: " + sc.locale() + " (country=" + sc.locale().getCountry() + ")");
            sc.useLocale(Locale.FRANCE);
            System.out.println("France:  " + sc.locale());
        }
    }
}

How It Works

  1. Explicitly set before printing. Calling useLocale ensures predictable output regardless of the host machine's default region.
  2. Locale components exposed. getLanguage() returns "en" and getCountry() returns "DE".
  3. Standard Locale.toString() tags. Java prints en_US, de_DE, and fr_FR.

Example 2 — Verify Locale.US Before Reading Portable Data

Ensure a helper method checks or enforces Locale.US so dot decimals parse uniformly.

Java
import java.util.Locale;
import java.util.Scanner;

public class ScannerLocaleGuard {
    static double readRate(Scanner sc) {
        if (!Locale.US.equals(sc.locale())) {
            sc.useLocale(Locale.US);
        }
        return sc.nextDouble();
    }

    public static void main(String[] args) {
        try (Scanner sc = new Scanner("4.75").useLocale(Locale.GERMANY)) {
            System.out.println("Before helper: " + sc.locale());
            double rate = readRate(sc);
            System.out.println("After helper:  " + sc.locale() + ", rate=" + rate);
        }
    }
}

How It Works

  1. Check before mutating. Comparing Locale.US.equals(sc.locale()) detects that the scanner is currently using de_DE.
  2. Switch automatically. Calling useLocale(Locale.US) inside the helper prevents InputMismatchException on "4.75".
  3. Both locale and rate confirmed. Printing sc.locale() afterwards shows en_US.

Example 3 — Save the Previous Locale and Restore It After Reading

Temporarily switch to Locale.GERMANY for one number, then put the original locale back.

Java
import java.util.Locale;
import java.util.Scanner;

public class ScannerLocaleRestore {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("4,25 9.5").useLocale(Locale.US)) {
            Locale saved = sc.locale();
            sc.useLocale(Locale.GERMANY);
            double germanVal = sc.nextDouble();
            sc.useLocale(saved);
            double usVal = sc.nextDouble();
            System.out.println("German value: " + germanVal);
            System.out.println("Restored? " + sc.locale().equals(saved) + ", next=" + usVal);
        }
    }
}

How It Works

  1. Store initial Locale object. Holding saved = sc.locale() remembers en_US without hardcoding it in multiple places.
  2. Temporary switch reads comma decimal. Under Locale.GERMANY, "4,25" parses as 4.25.
  3. Exact equality restored. Passing saved back lets "9.5" parse under the original locale.

Frequently Asked Questions

The JVM default format locale (from Locale.getDefault(Locale.Category.FORMAT)). Always call useLocale(Locale.US) when reading fixed dot-decimal files.
It prints en_US for Locale.US and de_DE for Locale.GERMANY.
It affects both: in base 10, nextInt() recognizes the locale grouping separator (comma in US, dot in Germany).
No. reset() reverts the scanner locale back to the default JVM locale.
No, a Scanner always has a non-null active Locale.

Next: match()

Return the MatchResult from the last successful scanning operation.

match() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful