This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
/**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();
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
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
while(quit!="the end")
should be while(!quit.equalsIgnoreCase("the end"))
unyime inok
Greenhorn
Joined: Oct 13, 2004
Posts: 29
posted
0
the program doesn't drop out of the loop when a user enters "the end", even with equalsIgnoreCase().
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
It doesn't drop out of the inner loop if there are still occurrences of "xxx" but it does quit OK when there are no more "xxx".
Your program doesn't seem to follow the specs at the top of your post, here's a few lines, just add the 'count' method (if this is what you're trying to do)