TextFormatter
class is a utility class (i.e., a
class that only has static methods) that can be used to format
String
objects in a variety of ways.
TextFormatter
class must have the following
public methods. It may contain other public and private methods
as well.
/** * Center a String in a field of given width. * * If the String is too wide, it is truncated to the appropriate * length (i.e., characters are removed from the right/end). * If the String can't be exactly centered, it leans to * the left. That is, if the String "1234" is centered in a field * of width seven, the result will be " 1234 ". * * @param string The String to center * @param width The width of the field * @return The centered String */ public static String center(String string, int width) /** * Construct a String representation of a dollar amount * (left justified in a field of given width, with grouping characters). * Note: The width behaves as it does in the String.format() method. * The resulting String does not include cents (i.e., * "100" not "100.00"). * * @param dollars The dollar amount * @param width The width of the field (excluding the $) */ public static String dollars(int dollars, int width) /** * Indent each line (of a potentially multi-line String, * delimited with '\n' characters) by a * given number of spaces. * * The String that is returned will not have a '\n' at the end of * the last line if the original String did not. * * @param string The String * @param indent The number of spaces to indent * @return An appropriately indented (multi-line) String */ public static String indent(String string, int indent) /** * Construct a String representation of an int, * left-justified in a field of given width. * If the int is negative, this method returns "NA" * left-justified in a field of the given width. * Note: The width behaves as it does in the String.format() method. * * @param value The int * @param width The width of the resulting String */ public static String positiveInt(int value, int width) /** * Construct a String consisting of a single character repeated * a given number of times. * * @param character The character * @param length The number of spaces * @return The String */ public static String repeat(char character, int length) /** * Wrap a String in a field of given width. That is, * create a (potentially) mult-line String that has at most * width characters per line. Line breaks will only occur * at "word" boundaries (i.e., at white space). Multiple white space * characters in a row will be treated as a single delimiter. * * The String that is returned will not have a '\n' at the end of * the last line if the original String did not. * * @param string The String to wrap * @param width The width of the field * @return The wrapped String (possibly containing multiple lines) */ public static String wrap(String string, int width) /** * Format a boolean as either "Yes" or "No " * * @param bool The boolean of interest * @return The String representation */ public static String yesNo(boolean bool)
Copyright 2011