Home Java Scanner Methods locale() Java Scanner locale() Method 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().
Scanner sc = new Scanner("3.5").useLocale(Locale.US);
Locale active = sc.locale(); // en_US
String lang = active.getLanguage(); // "en" Call What 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.util.Scanner
public Locale locale() Parameters Parameter Description None locale() takes no parameters.
Return Value Return Value Description LocaleThe scanner's current Locale.
Compare with Similar Methods Locale inspection alongside configuration:
Method How 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 1. Inspect locale tags 2. Verify US locale 3. Save and restore Example 1 — Inspect Active Locale Codes After Calling useLocale() Set three well-known locales in sequence and print their language, country, and string representations.
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());
}
}
} US: en_US (lang=en)
Germany: de_DE (country=DE)
France: fr_FR How It Works Explicitly set before printing. Calling useLocale ensures predictable output regardless of the host machine's default region.Locale components exposed. getLanguage() returns "en" and getCountry() returns "DE".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.
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);
}
}
} Before helper: de_DE
After helper: en_US, rate=4.75 How It Works Check before mutating. Comparing Locale.US.equals(sc.locale()) detects that the scanner is currently using de_DE.Switch automatically. Calling useLocale(Locale.US) inside the helper prevents InputMismatchException on "4.75".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.
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);
}
}
} German value: 4.25
Restored? true, next=9.5 How It Works Store initial Locale object. Holding saved = sc.locale() remembers en_US without hardcoding it in multiple places.Temporary switch reads comma decimal. Under Locale.GERMANY, "4,25" parses as 4.25.Exact equality restored. Passing saved back lets "9.5" parse under the original locale.Frequently Asked Questions What does Scanner.locale() return by default? The JVM default format locale (from Locale.getDefault(Locale.Category.FORMAT)). Always call useLocale(Locale.US) when reading fixed dot-decimal files.
What does sc.locale().toString() print for Locale.US and Locale.GERMANY? It prints en_US for Locale.US and de_DE for Locale.GERMANY.
Does locale() affect nextInt() or only nextDouble()? It affects both: in base 10, nextInt() recognizes the locale grouping separator (comma in US, dot in Germany).
Does reset() keep the locale set by useLocale()? No. reset() reverts the scanner locale back to the default JVM locale.
Can locale() return null? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion