My Utility Class

 

Overview

This class provides for many common programming needs, particularly the formatting of data for output. It is especially convenient for those new to Java.

 

Source code for the class

Source code for the class is part of the Test project described in the documentation on running Java programs.

 

Using the class

Call the desired class method being sure to pass it any required parameters. You may code the call anywhere the returned data type makes sense. A method that returns no value may be called as a stand-alone statement, such as

Utility.skip(2);

which skips 2 lines on the display screen in a non-graphics program. A method that returns a value may be called from within an expression, such as

System.out.println("Balance is " + Utility.moneyFormat(1234.56));

which converts the double value 1234.56 into the string "$1,234.56". The string is then appended to "Balance is " and the resulting string displayed on the screen in a non-graphics program.

Public fields of my Utility class are as follows:

ALIGN_CENTER

An int that specifies centering of a string within a print field when calling the printField() method listed below.

ALIGN_LEFT

An int that specifies left-aligning of a string within a print field when calling the printField() method listed below.

ALIGN_RIGHT

An int that specifies right-aligning of a string within a print field when calling the printField() method listed below.

Public methods of my Utility class are as follows:

String fixedFormat(double value)

Converts a double value (such as 4.321) into a string having proper fixed format with no decimal places (such as "4")

String fixedFormat(double value, int noDecimalPlaces)

Converts a double value (such as 4.321) into a string having proper fixed format with a specified number of decimal places (such as "4.3")

String moneyFormat(double amount)

Converts a double value (such as 1234.56) into a string having proper currency format (such as "$1,234.56")

String percentFormat(double value)

Converts a double value (such as .123) into a string having proper percent format with no decimal places (such as "12%")

String percentFormat(double value, int noDecimalPlaces)

Converts a double value (such as .123) into a string having proper percent format with a specified number of decimal places (such as "12.3%")

void pressEnterKey()

Prompts the user to "Press ENTER key to continue..." and waits for them to do so. Do not use in graphical programs.

void printField(int width, String value)

Prints a right-aligned string in a field of a specified width. Do not use in graphical programs.

void printField(int width, String value, int align)

Prints an aligned string in a field of a specified width. The string may aligned left, right, or center within the field. Do not use in graphical programs.

void separator(int sepLength, char sepChar)

Displays a separator (a line of characters) on the screen. The length of the line and the character used to construct it are received as parameters. Do not use in graphical programs.

void skip()

Skips one line on the screen. Do not use in graphical programs.

void skip(int noLines)

Skips the specified number of lines on the screen. Do not use in graphical programs.