The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Word filter assignment help

Hello, my assignment is to create a word filter that prompts for the forbidden word and replacement word, then scans an arbitrary amount of text and prints out the cleaned up version of the text, and also print the number of replaced words at the end. I understand the loop structure (I have a flow chart written out) but I am completely confused on the syntax of getting the program to scan individual words and then move on to the next after it gets a result. Do I need a variable that will act as a middle ground so it can do this or not?

Also I only need to account for full words. If night was the forbidden word I wouldn't have to replace it within the word knight.

Here's my code:

importjava.util.Scanner;publicclass Project2 {publicstaticvoid main(String[] args){String forbidword;String replaceword;int numreplaced =0;System.out.println("Enter word to be filtered out:");
Scanner keyboard =new Scanner(System.in);
forbidword = keyboard.next();System.out.println("Enter replacement word:");
replaceword = keyboard.next();System.out.println("Enter text to filter. Hit Control-Z to stop.");while(keyboard.hasNext());{if(keyboard.next().equals(forbidword))// I know /\ this /\ is wrong but I don't know what else to put there.{
numreplaced = numreplaced +1;// Supposed to somehow move on to the next word here? }else{// This will be the same as the 'if' part except without the counter}}System.out.println("A total of "+ numreplaced +" words were replaced.");}}

Re: Word filter assignment help

Read into a variable and then test it vs doing it all in one statement. It will allow for debugging and further processing of what was read in. Don't do it this way:
if (keyboard.next().equals(forbidword))

Re: Word filter assignment help

Originally Posted by Norm

Read into a variable and then test it vs doing it all in one statement. It will allow for debugging and further processing of what was read in. Don't do it this way:
if (keyboard.next().equals(forbidword))

I agree with Norm. I have always found that it is easier to debug and understand my programs when I create variables for things, whether I'm going to use it more than once or not. It is a good thing to do, especially in larger programs.

It is more lines of code, but it makes it easier to debug and fix when things go wrong.

Also, from a personal stylistic point of view, I recommend always initializing variables with basic values. I know some types have defaults, but I prefer to always initialize when I create.
For ints: I usually set them to -1, since that is usually used as an anomaly number.
For Strings: I usually set them to "" (empty String), since that will results in an empty value.
For Objects: I usually set them to null, since that indicates I never gave it a value.

I do this because it prevents compiler errors about not initializing variables when your variable setting is condition based. For you, this isnt the case, but it is a good thing to start doing.