Scanning
The Scanner class objects are used for breaking down formatted input into tokens and translating individual tokens according to their data type.Breaking Input into Tokens
By default, a scanner uses whitespace to separate tokens. To set a different token separator, we can use useDelimiter() method, along with a regular expression of the delimiter. To use comma (,) as a delimiter, we can use the following statement.s.useDelimiter(",\\s*");where 's' is a Scanner object.
Normally, all input tokens are considered to be String values. But Scanner class provides methods to support tokens of all other primitive types also.
Formatting
Stream objects that implement formatting are instances of either PrintWriter and PrintStream classes. Both the classes implement the same set of methods for converting internal data into formatted output. Two levels of formatting are provided:print and println Methods
Used to format individual values in a standard way
format method
This method formats almost any number of values based on a format string, with any options for precise formatting. The format string consists of static text embedded with format specifiers; except for the format specifiers, the format string is output unchanged. The API specification of Formatter class gives a complete description about the various usages of format method.
There are different format specifiers for different types of data outputs. The most common of them are %d, %f, %n etc. %n is used as line separator in JAVA. The format specifier contains the following elements:
- begin format specifier(mandatory)
- arguement index
- flas
- width
- precision
- conversion(mandatory)
tutorials
No comments:
Post a Comment