// Scott DeRuiter 7/11/2002 // Break.java // Take a sentence entered by the user and break it up into words. // 7/15/2011 - Greenstein: Changed TextReader to Scanner import java.util.Scanner; public class Break { private String sentence, word; public Break ( ) { sentence = ""; word = ""; } public static void main ( String [] args ) { Break b = new Break ( ); b.getSentence ( ); b.ridPunct ( ); b.separateAndPrint ( ); } public void getSentence ( ) { Scanner console = new Scanner ( System.in ); do { System.out.print ( "\n\nEnter a sentence: " ); sentence = console.nextLine ( ); } while ( sentence == null ); sentence = sentence.toLowerCase ( ); } public void separateAndPrint ( ) { int index = 0; boolean done = false; while ( !done ) { index = sentence.indexOf ( ' ' ); if ( index != -1 ) { word = sentence.substring ( 0, index ); sentence = sentence.substring ( index, sentence.length ( ) ); sentence = sentence.trim ( ); System.out.println ( word ); } else { done = true; System.out.println ( sentence ); } } } public void ridPunct ( ) { sentence = sentence.trim ( ); String help = ""; char letter = ' '; for ( int i = 0; i < sentence.length ( ); i++ ) { letter = sentence.charAt ( i ); if ( letter >= 'a' && letter <= 'z' ) help = help + letter; else if ( letter >= '0' && letter <= '9' ) help = help + letter; else if ( letter == ' ' || letter == '\t' ) help = help + ' '; } sentence = help; } }