By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,019 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,019 IT Pros & Developers. It's quick & easy.

creating filter processor

P: 2
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.print("> ");
String inputLine =
String[] wordArray = inputLine.split("[\\s]+");
Set<String> words = new HashSet<String>();
for (String word : wordArray) {
return words;

any help watsoever would be greatly appreciated!
Apr 26 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 10K+
P: 11,448
Note that a Scanner implements the Iterator<String> interface so you can
read words/tokens separated by spaces as follows:
Expand|Select|Wrap|Line Numbers
  1. int nofWords= 0;
  2. for (; myScanner.hasNext(); nofWords++) {
  3.    String token=;
  4.    if (token.equals("end")) break;
  5.    // here you pass token to Filter for further processing.
  6. }
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,

Apr 26 '07 #2

P: 2
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.HashSet;
public class UniqueFilter implements Filter
Set<String> words = new HashSet<String>();
int x;
public int getWordCount()
//prints line x
return x;
//returns the x value

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

//prints out "" if repetition
return s;
//returns string s


Thank you in advance for any help whatso ever!
Apr 29 '07 #3

Expert 10K+
P: 11,448
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:
Expand|Select|Wrap|Line Numbers
  1. public interface Filter {
  2.    public String process(String word);
  3. }
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:
Expand|Select|Wrap|Line Numbers
  1. public class UniqueFilter implements Filter {
  2.    private Set<String> set= new HashSet<String>();
  3.    // interface implementation:
  4.    public String process(String word) {
  5.       // return "" when adding fails (i.e. word was not unique
  6.       return set.add(word)?word:"";
  7.       }
  8.    }
  9. }
kind regards,


ps. I've given away too much code already; you should be able to handle it from here.
Apr 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.