Java Reference
In-Depth Information
}
}
If you compile and run this class, it prints the two demonstration line numbers centered, as
shown:
>
javac -d . StringAlignSimple.java
>
java strings.StringAlignSimple
- i -
4
>
Format
in the package
java.text
. There is a series of
Format
classes that all have at least
one method called
format()
. It is thus in a family with numerous other formatters, such as
DateFormat
,
NumberFormat
, and others, that we'll take a look at in upcoming chapters.
Example 3-5. StringAlign.java
public
public class
class
StringAlign
StringAlign
extends
extends
Format
{
private
private static
static final
final
long
long
serialVersionUID
=
1L
;
public
public enum
enum
Justify
{
/* Constant for left justification. */
LEFT
,
/* Constant for centering. */
CENTER
,
/** Constant for right-justified Strings. */
RIGHT
,
}
/** Current justification */
private
private
Justify just
;
/** Current max length */
private
private
int
int
maxChars
;
/** Construct a StringAlign formatter; length and alignment are
* passed to the Constructor instead of each format() call as the
* expected common use is in repetitive formatting e.g., page numbers.
* @param maxChars - the maximum length of the output
* @param just - one of the enum values LEFT, CENTER or RIGHT
*/