MsgBox3.java
Download MsgBox3.java
1: // MsgBox3 - Displays Strings in a boxes. Non-GUI stand-alone Java
2: // program. This version shows the creation and use of two methods.
3: //
4: // Written by Wayne Pollock, Tampa, FL USA, 2000
5: // Modified by Wayne Pollock 2005 to use Java Scanner class.
6: // Modified by Wayne Pollock 2014 to count Unicode characters correctly.
7:
8: import java.util.Scanner;
9:
10: class MsgBox3
11: {
12: public static void main ( String [] args )
13: {
14: Scanner in = new Scanner( System.in );
15:
16: for ( ;; )
17: {
18: System.out.print( "Enter your Message (Hit Enter to quit): " );
19: if ( ! in.hasNextLine() )
20: break; // Quit on EOF
21:
22: String message = in.nextLine();
23:
24: if ( message.length() == 0 )
25: break; // Quit on empty message (or EOF)
26:
27: boxMsg( message );
28: }
29: }
30:
31:
32: public static void boxMsg ( String msg )
33: {
34: int length = msg.codePointCount( 0, msg.length() );
35: drawLine( length ); // Draw the top of the box
36: System.out.println( "\t|" + msg + "|" );
37: drawLine( length ); // Draw the bottom of the box
38: }
39:
40:
41: public static void drawLine ( int width )
42: {
43: System.out.print( "\t+" );
44: for ( int i = 0; i < width; ++i )
45: System.out.print( "-" );
46: System.out.println( "+" );
47: }
48: }