Flash cards
Review the key moves
What is the main idea behind Java Scanner useLocale() Method?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
// ___ a scanner object Scanner myObj = new Scanner("1.500.000");Put the learning moves in the order that makes the concept easiest to apply.
❮ Scanner Methods
Read numbers from a different locale
// Create a scanner object Scanner myObj = new Scanner("1.500.000");
// Change delimiter myObj.useLocale(new Locale("es"));
// Read and display the number System.out.println(myObj.nextInt());Definition and Usage
The useLocale() method changes the locale used by the scanner. The locale determines how numbers are interpreted by deciding how digits are grouped, which character serves as a decimal point, etc.
Locale objects
The useLocale() method requires a Locale object as an argument. Locale objects represent a language or country and they are used by a variety of Java classes to handle formatting and interpreting data.
The easiest way to get a Locale object is by using one of the objects provided by attributes of the Locale class.
myObj.useLocale(Locale.GERMANY));A list of available language and country attributes is shown below.
| Locale.CANADA Locale.CANADA_FRENCH Locale.CHINA Locale.FRANCE Locale.GERMANY Locale.ITALY Locale.JAPAN Locale.KOREA Locale.PRC Locale.TAIWAN Locale.UK Locale.US | Locale.CHINESE Locale.ENGLISH Locale.FRENCH Locale.GERMAN Locale.ITALIAN Locale.JAPANESE Locale.KOREAN Locale.SIMPLIFIED_CHINESE Locale.TRADITIONAL_CHINESE |
|---|
If the country or language you need is not in the list then you can create a new Locale object using a language code and an optional country code. Most codes are two or three characters long and each code represents a language or a country.
Locale spanish = new Locale("es");
Locale spain = new Locale("es", "ES");Syntax
public Scanner useLocale(Locale
locale )Parameter Values
| Parameter | Description |
|---|---|
| locale | Required. A Locale object. |
Technical Details
| Returns: | A reference to the Scanner object that this method belongs to, which allows for chaining configuration methods. An example of chaining is myObj.useLocale(Language.GERMAN).useDelimiter(","); . |
|---|
❮ Scanner Methods