Java Reference
In-Depth Information
It is no accident that these two methods are paired. Although a Scanner
can read many formats of data, it is best used for data formatted in re-
latively straightforward ways. Stream mode is useful for reading user
input as well, but when you use Scanner for data you should be able to
visualize (if not actually write) the printf usage that would generate that
data. Scanner is extremely powerful, but that power can be difficult to
harness.
Exercise 22.10 : Write a method to tokenize input that ignores com-
ments, using the comment pattern as part of the scanner's delimiter.
Exercise 22.11 : Write a version of readCSV that uses a StreamTokenizer
rather than a Scanner .
Exercise 22.12 : Write a version of the attribute reading method from
page 533 that uses a Scanner . For this exercise it is only necessary that
both versions accept the same correctly formatted input.
Exercise 22.13 : Extend your solution to Exercise 22.12 so that mis-
placed = characters are detected, as in the StreamTokenizer version.
(Hint: You might want to try to dynamically change the delimiter pattern
between certain tokens.)
22.5.4. Localization
Localization is covered in detail in Chapter 24 , but since the Scanner class
has some support for localization, we cover that briefly here.
By default a scanner works in the current locale, but you can change it
with the useLocale method, which takes a Locale instance. The locale be-
ing used by the scanner can be retrieved from the locale method.
The type-specific numeric scanning methods, like hasNextDouble and
nextInt , are fully localized. They recognize numerical values that are
formed from the numeric digit characters of the locale being used, in-
cluding the locale-specific decimal separator and grouping separator.
In contrast, to use a regular expression to match a local number, you
would need to know what the separator characters were so that you
 
Search WWH ::




Custom Search