MsgBox2.java

Download MsgBox2.java

 1: // MsgBox2 - Displays a String in a box.  Non-GUI stand-alone Java
 2: // program.  This version demonstrates the creation and use of a method.
 3: //
 4: // Written by Wayne Pollock, Tampa, FL USA, 2000
 5: // Modified by Wayne Pollock, 2005, to use Java5 Scanner class.
 6: 
 7: import java.util.Scanner;
 8: 
 9: class MsgBox2
10: {
11:    public static void main ( String [] args )
12:    {
13:       Scanner in = new Scanner( System.in );
14: 
15:       System.out.print( "Enter your Message: " );
16: 
17:       if ( ! in.hasNextLine() )
18:          return;  // All done on EOF, so just exit.
19: 
20:       String message = in.nextLine();
21: 
22:       int msgLength = message.length();
23: 
24:       drawLine( msgLength );  // Draw the top of the box
25: 
26:       // Print the message:
27:       System.out.println( "\t|" + message + "|" );
28: 
29:       drawLine( msgLength );  // Draw the bottom of the box
30:    }
31: 
32: 
33:    public static void drawLine ( int width )
34:    {
35:       System.out.print( "\t+" );
36:       for ( int i = 0; i < width; ++i )
37:          System.out.print( "-" );
38:       System.out.println( "+" );
39:    }
40: }