creating filter processor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mr 200
    New Member
    • Apr 2007
    • 2

    #1

    creating filter processor

    Hi all

    ive just recently started learning java and was happy to find a forum that may be able to give me a little assistance. I have been set an excersize to create a filter processor that relays information between several filters to give a result. I have created one of the filters already, however i need the filter processor working in order to test wether the filter i have done works correctly. However im not completely sure where to start with it, i know that i have to import scanner and use tokenizing within the code but where exactly to start is whats giving me some trouble. Here is the excersize question ive been given

    Write a FilterProcessor class. It should accepts a Filter object in its constructor.
    It should also have a go() method.
    This method should read from standard input, break the input into words
    (space/newline separated) and pass each word to the filter.
    The string returned by the filter should be printed to the console.
    The go() method should stop when it sees the word “end”.
    It should then print the number of words processed.

    I have the code for input scanner in there already but im not sure how to use tokenizing to suit the task. eg

    public Set<String> getInput()
    { System.out.prin t("> ");
    String inputLine =
    reader.nextLine ().trim().toLow erCase();
    String[] wordArray = inputLine.split ("[\\s]+");
    Set<String> words = new HashSet<String> ();
    for (String word : wordArray) {
    words.add(word) ;
    }
    return words;
    }

    any help watsoever would be greatly appreciated!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Note that a Scanner implements the Iterator<String > interface so you can
    read words/tokens separated by spaces as follows:
    Code:
    int nofWords= 0;
    for (; myScanner.hasNext(); nofWords++) {
       String token= myScanner.next();
       if (token.equals("end")) break;
       // here you pass token to Filter for further processing.
    }
    I don't know what a Filter object is supposed to do and when it is supposed
    to return a String or whatever ...

    kind regards,

    Jos

    Comment

    • Mr 200
      New Member
      • Apr 2007
      • 2

      #3
      thanks for that info jos

      also im having a bit of a problem with my unique filter, it is spose 2 operate so that if i type badger badger snake, into the termina window and execute the program it will return "badger" " " "snake". deleting the doubled word... however if i type that in i get ">" ">" ">"

      here is my code

      import java.util.Set;
      import java.util.HashS et;
      public class UniqueFilter implements Filter
      {
      Set<String> words = new HashSet<String> ();
      int x;
      public int getWordCount()
      {
      System.out.prin tln(x);
      //prints line x
      return x;
      //returns the x value
      }

      public String process(String s)
      //
      {
      {
      if (words.contains (s))
      //if words has s then..
      {
      words.add(s);
      //add words to s
      x++;
      //increment x by a value of 1
      System.out.prin tln(s);
      // prints out string s
      return s;
      //returns string s

      }
      else
      {
      System.out.prin tln("");
      //prints out "" if repetition
      return s;
      //returns string s
      }

      }
      }
      }

      Thank you in advance for any help whatso ever!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        As far as I understand the description of your assignment (see your first post in
        this thread) a Filter should keep its mouth shut. The FilterProcessor should
        print things returned by the Filter and count the words read. It should print that
        number after the word "end" is read. My first snippet of code counts all the
        words (see the 'nofWords' variable). I assume there's some interface defined,
        like this one:
        Code:
        public interface Filter {
           public String process(String word);
        }
        Indeed its a good idea to keep words in a Set in your UniqueFilter but I don't
        understand your logic when the Set already contains a word, i.e. you try to add
        it to the Set again and your print it out and even increment a variable 'x'.

        What for? The FilterProcessor counts words and does the printing. IMHO a
        UniqueFilter should just do this:
        Code:
        public class UniqueFilter implements Filter {
           private Set<String> set= new HashSet<String>();
           // interface implementation:
           public String process(String word) {
              // return "" when adding fails (i.e. word was not unique
              return set.add(word)?word:"";
              }
           }
        }
        kind regards,

        Jos

        ps. I've given away too much code already; you should be able to handle it from here.
        Last edited by JosAH; Apr 29 '07, 01:25 PM. Reason: typo ...

        Comment

        Working...