Java Scanner useLocale() Method

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

Definition

In Java, sc.useLocale(locale) tells the scanner which regional rules to use when parsing numbers, and returns the Scanner itself so calls can be chained.

It switches decimal marks (dot vs comma) and thousands grouping characters: for example, Locale.GERMANY turns "3,5" into 3.5 and "1.234,5" into 1234.5.

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.useLocale(...).

Syntax

Import java.util.Locale and chain useLocale(...) right after creating your scanner.

Java
Scanner sc = new Scanner("1.234,5").useLocale(Locale.GERMANY);
double val = sc.nextDouble();   // 1234.5
CallWhat it does
sc.useLocale(Locale.US)Uses . for decimals and , for thousands grouping.
sc.useLocale(Locale.GERMANY)Uses , for decimals and . for thousands grouping.
sc.useLocale(Locale.FRANCE)Uses , for decimals in French-formatted input.

Calling useLocale(Locale.US) is standard defensive practice whenever parsing machine-generated files, CSV exports, or tutorial inputs containing dot decimals.

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

Parameters

ParameterDescription
localeA Locale instance (such as Locale.US or Locale.GERMANY) defining number syntax.

Return Value

Return ValueDescription
ScannerThe same scanner instance, for convenient method chaining.
ThrowsNothing. It does not check whether the scanner is closed.

Compare with Similar Methods

Locale switching and related scanner settings:

MethodHow it differs
locale()Returns the currently active Locale.
useDelimiter(p)Sets token boundaries (often chained with useLocale).
useRadix(r)Changes number base (only affects integer parsing).
reset()Restores default locale, delimiter, and radix.

Java Examples

Example 1 — Parse German Decimals and Grouped Numbers with Locale.GERMANY

Set Locale.GERMANY to read "3,5" and "1.234,5" as doubles.

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

public class ScannerUseLocaleGermany {
    public static void main(String[] args) {
        String deText = "3,5 1.234,5";
        try (Scanner sc = new Scanner(deText).useLocale(Locale.GERMANY)) {
            double small = sc.nextDouble();
            double grouped = sc.nextDouble();
            System.out.println("3,5 -> " + small);
            System.out.println("1.234,5 -> " + grouped);
        }
    }
}

How It Works

  1. Comma as decimal point. Under Locale.GERMANY, "3,5" parses cleanly into the double 3.5.
  2. Dot as thousands grouping. In "1.234,5", the dot groups thousands and the comma marks the decimal fraction, giving 1234.5.
  3. Output always uses Java dot notation. Printing doubles in Java always displays a standard decimal dot regardless of input locale.

Example 2 — Contrast Grouped Decimals in Locale.US and Locale.GERMANY

Read a US-formatted total and a German-formatted total within one program by switching locales mid-scan.

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

public class ScannerUseLocaleContrast {
    public static void main(String[] args) {
        String mixed = "1,234.50 1.234,50";
        try (Scanner sc = new Scanner(mixed).useLocale(Locale.US)) {
            double usAmount = sc.nextDouble();
            sc.useLocale(Locale.GERMANY);
            double deAmount = sc.nextDouble();
            System.out.println("US amount: " + usAmount);
            System.out.println("DE amount: " + deAmount);
            System.out.println("Equal? " + (usAmount == deAmount));
        }
    }
}

How It Works

  1. US rule first. 1,234.50 uses comma for thousands and dot for cents, reading as 1234.5.
  2. Switch before second token. Changing to Locale.GERMANY interprets 1.234,50 using inverted symbols.
  3. Identical numeric result. Both tokens yield the exact same double value 1234.5.

Example 3 — Chain useDelimiter() and useLocale() on Semicolon CSV

Parse European semicolon-separated data (where commas are decimals!) by chaining both settings.

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

public class ScannerUseLocaleCsv {
    public static void main(String[] args) {
        String csv = "Coffee;3,50;2";
        try (Scanner sc = new Scanner(csv).useDelimiter(";").useLocale(Locale.FRANCE)) {
            String item = sc.next();
            double unitPrice = sc.nextDouble();
            int qty = sc.nextInt();
            System.out.println(qty + " x " + item + " = " + (unitPrice * qty));
        }
    }
}

How It Works

  1. Fluent builder chaining. Both useDelimiter(";") and useLocale(Locale.FRANCE) return the same scanner object.
  2. Comma preserved inside tokens. Splitting on ; leaves "3,50" intact for nextDouble() to parse under French rules.
  3. Exact product. Multiplying 3.5 by 2 prints 7.0.

Frequently Asked Questions

It returns the Scanner itself so you can chain it onto new Scanner(...) or useDelimiter(...).
Without it, Scanner uses the JVM default locale, so dot decimals like 3.5 throw InputMismatchException on systems set to German or French regions.
Yes, and it parses "1.234,5" as 1234.5 because German formatting uses comma for decimals and dot for thousands.
No. System.out.println(double) always prints numbers with a dot, regardless of the scanner locale.
Yes, in base 10 it determines which grouping separator (such as comma in US or dot in Germany) is accepted inside integer tokens.

Next: useRadix()

Change the default number base (2 to 36) for integer reads.

useRadix() 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