// Words.java (c) 2004 Kari Laitinen // http://www.naturalprogramming.com // 2004-10-09 File created. // 2004-12-19 Last modification. // This program is probably too difficult for a beginner // in computer programmigng. So, don't panic if you cannot // understand everything about this while you are still // studying the subjects of Chapter 6. // If you do not put the period to the end of the sentence // that you give to this program, the program won't stop. // In emergency, you can type Ctrl-C to stop the program. // You type Ctrl-C when you press the Ctrl and C keys // simultaneously. // WordsBetter.java in the javafilesextra folder is a // program that works more exactly like the words.cpp and // Words.cs programs in my C++ and C# books. import java.util.* ; class Words { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n This program separates the words of a" + "\n sentence and prints them in wide form." + "\n Type in a sentence, and write a PERIOD '.'" + "\n at the end of the sentence.\n\n " ) ; String character_in_sentence = keyboard.findInLine( "." ) ; System.out.print( "\n " ) ; while ( character_in_sentence.charAt( 0 ) != '.' ) { if ( character_in_sentence.charAt( 0 ) == ' ' ) { System.out.print( "\n " ) ; } else { System.out.print( " " + character_in_sentence ) ; } character_in_sentence = keyboard.findInLine( "." ) ; } } } /* Multiline comment: findInLine() method ignores delimiters. The pattern specified by string "." means that any character is accepted. In this program "." does not match line termination characters. For more information, see WordsBetter.cs in javafilesextra. "[\\d\\s]" would mean any digit or any whitespace character. */