/**Counting the appearances of a given
word in a text
We have seen how to read in and manipulate characters and character strings and how to use the classes
String and StringTokenizer. Use these to read in a word and then other text from the keyboard. Count the occurrences of the word that you read in at the beginning. Your program should be case insensitive; this means that
you should convert the original word to pure upper case OR lower case and compare each token that you separate to this same case using toUpperCase() or toLowerCase(). Your program should keep checking text until you type �The End� by itself on one line**/
import java.io.*;
import java.util.*;
//Inserts word into text and breaks the text into tokens
public class WordText{
String word = new String();
String text = new String();
//Allows user to enter input from the keyboard
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader keyb = new BufferedReader(isr);
//sets word
public void setWord(String w)
{
word = w;
}
//gets word
public String getWord() throws IOException
{
return word;
}
//sets text
public void setText(String t)
{
text = t;
}
//gets text
public String getText() throws IOException
{
return text;
}
//Edits the text entered
public static void editText(String word, String text) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader keyb = new BufferedReader(isr);
String edittedText = new String(),
quit = new String();
int indexOfxxx;
while(quit!="the end")
{
System.out.println("Type 'the end' to quit checking");
quit = keyb.readLine();
while((indexOfxxx = text.indexOf("xxx"))!= -1)
{
edittedText = text.substring(0,indexOfxxx);
edittedText = edittedText.concat(word);
edittedText = edittedText.concat(text.substring(indexOfxxx+3));
text = edittedText;
indexOfxxx =text.indexOf("xxx");
System.out.println(edittedText.toUpperCase());
System.out.println("Type 'the end' to quit checking");
quit = keyb.readLine();
//Breaks text into tokens
StringTokenizer st = new StringTokenizer(edittedText);
while(st.hasMoreTokens())
{
System.out.println(st.nextToken().toUpperCase());
}
}//end of inner-while loop
}//end of outer-while loop
}
//Prompts user for input and reads it
public static void readInput()throws IOException
{
WordText wt = new WordText();
System.out.println("Enter a word");
wt.word = wt.keyb.readLine();
wt.getWord();
System.out.println("Please enter the text with xxx that you want to replace");
wt.text = wt.keyb.readLine();
wt.getText();
wt.editText(wt.word,wt.text);
}
//Main method
public static void main(String [] args)throws IOException
{
readInput();
}
}//end of class