MsgBox2.java

Download MsgBox2.java source file

// MsgBox2 - Displays a String in a box.  Non-GUI stand-alone Java
// program.  This version demonstrates the creation and use of a method.
//
// Written by Wayne Pollock, Tampa, FL USA, 2000
// Modified by Wayne Pollock, 2005, to use Java5 Scanner class.

import java.util.Scanner;

class MsgBox2
{
   public static void main ( String [] args )
   {
      Scanner in = new Scanner( System.in );

      System.out.print( "Enter your Message: " );

      if ( ! in.hasNextLine() )
         return;  // All done on EOF, so just exit.

      String message = in.nextLine();

      int msgLength = message.length();

      drawLine( msgLength );  // Draw the top of the box

      // Print the message:
      System.out.println( "\t|" + message + "|" );

      drawLine( msgLength );  // Draw the bottom of the box
   }


   public static void drawLine ( int width )
   {
      System.out.print( "\t+" );
      for ( int i = 0; i < width; ++i )
         System.out.print( "-" );
      System.out.println( "+" );
   }
}