Advertisement
brilliant_moves

WelcomeMessage.java

Aug 12th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class WelcomeMessage {
  4.  
  5.     /**
  6.     *   Program:    WelcomeMessage.java
  7.     *   Purpose:    Display custom welcome message in command prompt window
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    12.08.2014
  10.     */
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         final int BOX_WIDTH = 80; // width of welcome message box = DOS command prompt width
  15.         final int BOX_HEIGHT = 9; // height of welcome message box
  16.  
  17.         Scanner scan = new Scanner(System.in);
  18.         String message;
  19.         char symbol = '#';
  20.         int leadingSpaces, trailingSpaces;
  21.  
  22.         do {
  23.             System.out.print("Enter your welcome message: ");
  24.             message = scan.nextLine();
  25.             if (message.length() > BOX_WIDTH-4) {
  26.                 System.out.println("Message too long!");
  27.             } // end if
  28.             if (message.length() < 1) {
  29.                 System.out.println("No message!");
  30.             } // end if
  31.         } while (message.length() > BOX_WIDTH-4 || message.length() < 1);
  32.  
  33.         System.out.println("\n");
  34.  
  35.         leadingSpaces = (BOX_WIDTH / 2) - (message.length() / 2);
  36.         trailingSpaces = (BOX_WIDTH - (leadingSpaces+message.length()));
  37.  
  38.         for (int row = 0; row<BOX_HEIGHT; row++) {
  39.             if (row==0 || row==BOX_HEIGHT-1) {  // first and last rows
  40.                 for (int col=0; col<BOX_WIDTH; col++) {
  41.                     System.out.print(symbol);
  42.                 } // end for col
  43.             } else if (row==BOX_HEIGHT/2) {     // middle row
  44.                 System.out.print(symbol);
  45.                 for (int sp = 1; sp<leadingSpaces; sp++) {
  46.                     System.out.print(" ");
  47.                 } // end for
  48.                 System.out.print(message);
  49.                 for (int sp = 1; sp<trailingSpaces; sp++) {
  50.                     System.out.print(" ");
  51.                 } // end for
  52.                 System.out.print(symbol);
  53.             } else {                // other rows
  54.                 System.out.print(symbol);
  55.                 for (int sp = 1; sp<BOX_WIDTH-1; sp++) {
  56.                     System.out.print(" ");
  57.                 } // end for
  58.                 System.out.print(symbol);
  59.             } // end if row
  60.         } // end for row
  61.     } // end main()
  62. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement