Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Word filter assignment help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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:
    import java.util.Scanner;
    public class Project2 {
     
    	public static void 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.");
    	}
     
    }
    Thank you!
    Last edited by helloworld922; September 15th, 2010 at 12:21 AM.


  2. #2
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Word filter assignment help

    Here's a sample output:

    Enter word to be filtered out:
    ni
    Enter word to replace it with:
    herring
    Enter text to filter. Hit Control-Z to stop.
    The wet dog ni
    sighs by the dry ni fire
    ni at night.
    The
    wet
    dog
    herring
    sighs
    by
    the
    dry
    herring
    fire
    herring
    at
    night.

    Replaced "ni" with "herring" 3 times.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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))

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Word filter assignment help

    Quote Originally Posted by Norm View Post
    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.

    You would do something like:
    String var = keyboard.next();
    System.out.println(var);
    if (var.equals(forbidword))

    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.

    So, instead of this:
    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();

    I would say:
    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();

    Once against, entirely stylistic, but it is how I prefer to do it.
    Last edited by aussiemcgr; September 15th, 2010 at 07:51 AM.

Similar Threads

  1. [SOLVED] Word Scramble Using Objects & Methods
    By Whitechapel in forum Object Oriented Programming
    Replies: 3
    Last Post: May 23rd, 2010, 12:38 PM
  2. Reading the size (height and width) of word documents
    By pradeep_das in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 7th, 2010, 09:58 AM
  3. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  4. How to invert a word String
    By Truffy in forum Java Programming Tutorials
    Replies: 16
    Last Post: October 3rd, 2009, 02:44 AM
  5. Internet Filter to display some website
    By sundarjothi in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 15th, 2008, 05:03 AM