Source of Utilities.java


  2: import java.util.Scanner;

  4: /**
  5:  * A class for some helpful input and output methods.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class Utilities {

 11:     public static final Scanner KBD = new Scanner(System.in);
 12:     private static final int SCREEN_WIDTH = 76;

 14:     /**
 15:      * Get a Scanner defined on System.in. The intent is that there should be
 16:      * only one Scanner connected to System.in for the whole program -- so we
 17:      * can avoid buffering issues that arise when we have multiple Scanners
 18:      * connected to a single source.
 19:      *
 20:      * @return the Scanner from this class that's connected to System.in
 21:      */
 22:     public static Scanner getKeyboard() {
 23:         return KBD;
 24:     }

 26:     /**
 27:      * Prompt the user and wait for them to press the enter key. Add blank lines
 28:      * before and after the prompt.
 29:      */
 30:     public static void pause() {
 31:         System.out.print("\n...press enter...");
 32:         KBD.nextLine();
 33:         System.out.println();
 34:     }

 36:     /**
 37:      * Print a title.
 38:      * <p>
 39:      * The title is underlined with hyphens, with blank lines before and after.
 40:      * The capitalization of the title is not changed for printing.
 41:      *
 42:      * @param title the title to be printed
 43:      */
 44:     public static void printTitle(String title) {
 45:         printTitle(title, '-');
 46:     }

 48:     /**
 49:      * Print a title using a particular underlining character.
 50:      * <p>
 51:      * The title is underlined with the given character, with blank lines before
 52:      * and after. The capitalization of the title is not changed for printing.
 53:      *
 54:      * @param title the title to be printed
 55:      * @param under the character to use to underline the title
 56:      */
 57:     public static void printTitle(String title, char under) {
 58:         // print blank line before
 59:         System.out.println();

 61:         // print the title and underline it
 62:         System.out.println(title);
 63:         for (int i = 1; i <= title.length(); ++i) {
 64:             System.out.print(under);
 65:         }
 66:         System.out.println();

 68:         // print blank line after
 69:         System.out.println();
 70:     }

 72:     /**
 73:      * Print a paragraph, wrapped to SCREEN_WIDTH characters per line.
 74:      * <p>
 75:      * The paragraph has a blank line printed after it. If the text contains a
 76:      * word of over SCREEN_WIDTH characters, that word will overflow the right
 77:      * edge of the paragraph. (This method does not hyphenate words.)
 78:      * <p>
 79:      * The method assumes that the line it starts on was empty. If it is not
 80:      * empty, the first line of the paragraph may overflow the right margin.
 81:      *
 82:      * @param text the text of the paragraph to be printed.
 83:      */
 84:     public static void printParagraph(String text) {
 85:         // create variable for how many characters are on the current line
 86:         int usedSoFar = 0;

 88:         // break string into words
 89:         String[] words = text.split(" ");

 91:         // for each word
 92:         for (int i = 0; i < words.length; ++i) {
 93:             // need to print every character of the word, plus a space
 94:             int spaceNeeded = words[i].length() + 1;

 96:             // if there's not enuf space for this word
 97:             if (usedSoFar + spaceNeeded > SCREEN_WIDTH) {
 98:                 // end the line, giving us a new, blank line
 99:                 System.out.println();
100:                 usedSoFar = 0;
101:             }

103:             // print the word; update the number of characters on this line
104:             System.out.print(words[i] + " ");
105:             usedSoFar += spaceNeeded;
106:         }
107:         // end the last line of the paragraph
108:         System.out.println();

110:         // print a blank line
111:         System.out.println();
112:     }

114: }