This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
For regular expressions, check out the API page for java.util.Scanner. Also Chapter 8 of the 5.0 rev of "Complete Java 2 Certification". Here's an example, reprinted with author's permission
The output is:
bcd fgh jkl
See how the vowels have been used as delimiters?
For more richly-defined pattern matching, you can use the quantifiers * + and ?. * matches zero or more occurrences of the preceding string. So "(ab)*" matches an empty string, "abab", "abababab", and so on. The + quantifier matches one or more occurrences, and the ? quantifier matches zero or one occurrences.
In certain arcane situations, it's hard to decide when a quantifier should stop matching. Should it match the longest possible string, or should it match something shorter? A greedy quantifier, which is the only kind covered in the exam, is the simplest: it matches the longest possible string. Reluctant and possissive quantifiers stop short of the longest match, using subtle algorithms that fortunately are WAY beyond the scope of the SCJP exam.
The Locale class is used for formatting text whose appearance depends on the part of the world where the text will be read. A simple example involves floating-point numbers. In the US and much of Asia, pi is written as 3.14159. But in many European countries the decimal "point" is actually a "comma", and pi is written as 3,14159. The following code prints pi, using the current machine's default locale:
The first string arg describes the format: a floating-point number occupying 7 characters, with 5 of those characters to the right of the decimal point/comma. I live in the US, so my output is "3.14159".
You can specify a locale like this:
Now the output is "3,14159".
Locales are especially good for formatting numbers, dates, and currency. These are all things that vary a lot from one locale to another. Again, there's more about this in the new "Complete Java 2 Certification".
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".