// Find.java // Prompts the user to enter a "word", then finds the index values // where this word occurs in a larger, pre-defined String // 7/15/2011 - Greenstein: Changed TextReader to Scanner import java.util.Scanner; public class Find { private String paragraph, word; public Find ( ) { paragraph = "Javajava javac javalin RJAva havajava JavA."; word = ""; } public static void main ( String [] args ) { Find f = new Find ( ); f.getWord ( ); f.findAndPrint ( ); } public void getWord ( ) { Scanner console = new Scanner ( System.in ); System.out.println ( "\n\n" + paragraph + "\n\n" ); do { System.out.print ( "Enter a word to search for: " ); word = console.nextLine ( ); } while ( word == null ); } public void findAndPrint ( ) { word = word.toLowerCase ( ); paragraph = paragraph.toLowerCase ( ); String original = paragraph; boolean done = false; int index = 0; while ( !done ) { index += paragraph.indexOf ( word ); if ( paragraph.indexOf ( word ) != -1 ) { System.out.println ( index ); paragraph = original.substring ( index + word.length ( ) ); index += word.length ( ); } else done = true; } } }