views:

77

answers:

1

I have this StreamTokenizer Iterator Adapter that is suppose to create a Tokenizer Iterator Index Builder then build the index from a STIA wrapped around a StreamTokenizer. I am having trouble implementing the hasNext and Next for my STIA, can anyone help me, here is my class:

public class StreamTokenizerIteratorAdapter implements Iterator<Token> {

 DefaultIndexImpl index;
 StreamTokenizer source;

 public StreamTokenizerIteratorAdapter(final StreamTokenizer source) {
  if (source == null)
   throw new IllegalArgumentException("source == null");



 }


 @Override
 public boolean hasNext() {

  return !index.isEmpty();
 }


 public Token next() {

  if(!index.isEmpty())
   return next();
  else
  return null;
 }


 @Override
 public void remove() {
  throw new UnsupportedOperationException();
 }
}

Should I be using the source element in the hasNext() and next()?

A: 

I have revised the class slightly but having trouble with the add method.

public class StreamTokenizerIteratorAdapter implements Iterator<Token> {

    StreamTokenizer s;

    public StreamTokenizerIteratorAdapter(final StreamTokenizer source) {
        if (source == null)
            throw new IllegalArgumentException("source == null");

        s = (StreamTokenizer) source;

    }


    @Override
    public boolean hasNext() {

        int temptoken;
        while ((temptoken = s.nextToken()) != StreamTokenizer.TT_EOF) {
            if (temptoken == StreamTokenizer.TT_WORD) {
                Token t = new DefaultToken(s.sval, s.lineno());
                s.add(t.getWord(),t.getLine()); //run-time error at add
            }
        }
    }


    public Token next() {

        return temptoken;
    }


    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}

The problem is that add method in add(t.getWord(), t.getLine()) is not being recognized by StreamTokenizer. How do I use the add method in a StreamTokenizer. Also is my next() correct?

Are you sure you aren't getting a compile-time error? StreamTokenizer doesn't have an add() method. Also, the compiler would give you an error on the hasNext() method since it never returns a boolean.
Rob Heiser