是否可以从java.util.Scanner创建lookAheadBuffer?

是否可以从java.util.Scanner创建lookAheadBuffer?,java,java.util.scanner,Java,Java.util.scanner,我想基于java.util.Scanner的当前状态构建一个前瞻缓冲区。我的意思是,如果你认为扫描仪是传送“文字”的传送带,那么扫描仪的“当前状态”就是传送带上剩余的文字 我想我可以通过构建另一个临时扫描仪来实现这一点,该扫描仪保持原始扫描仪的当前状态。然后,我可以使用getNext()从临时扫描仪填充前瞻缓冲区,而不影响原始扫描仪的状态 不幸的是,没有构造函数或方法提供我需要的原始扫描仪的当前状态 是否可以从扫描仪的当前状态创建一个前瞻缓冲区?正如大多数回复所表明的,扫描仪没有前瞻功能。如果这

我想基于java.util.Scanner的当前状态构建一个前瞻缓冲区。我的意思是,如果你认为扫描仪是传送“文字”的传送带,那么扫描仪的“当前状态”就是传送带上剩余的文字

我想我可以通过构建另一个临时扫描仪来实现这一点,该扫描仪保持原始扫描仪的当前状态。然后,我可以使用getNext()从临时扫描仪填充前瞻缓冲区,而不影响原始扫描仪的状态

不幸的是,没有构造函数或方法提供我需要的原始扫描仪的当前状态


是否可以从扫描仪的当前状态创建一个前瞻缓冲区?

正如大多数回复所表明的,扫描仪没有前瞻功能。如果这里有人感兴趣,我会告诉你我是怎么做的。这三个方法位于名为ScannableDocument的类中

    /**
     * Return the next word in the document.  Just a wrapper for Scanner and look ahead buffer methods.
     * The caller does not have to call hasNext().  
     * The caller does have to catch DocumentExhaustedException.
     * @return next word from scanner
     * @throws DocumentExhaustedException
     */
public String getNextWord() throws DocumentExhaustedException {

        // if the lookAheadBuffer is not empty, return its first element (fifo queue) instead of moving the scanner
        if ( !lookAheadBuffer.isEmpty()) {
            return lookAheadBuffer.removeFirst() ;
        } else {
            if ( scanner.hasNext()) {
                return scanner.next();
            } else {
                throw new DocumentExhaustedException("ScannableDocument.getNextWord()") ;
            }
        }
    }

    /**
     * Move numWords off of the conveyer.  
     * @param numWords
     * @throws DocumentExhaustedException
     */
    public void advanceConveyer ( int numWords ) throws DocumentExhaustedException {
        for ( int word=1; word<=numWords ; word++) {
            getNextWord() ;
        }
    }


    /**
     * The look ahead buffer is used to recognize patterns in the document that start with the 'current' word
     * The patterns come from a dictionary.  The look ahead buffer is loaded
     * with dictionaryEntry.size() words to enable a String comparison.

     * @param numWords
     */
    public void loadLookAheadBuffer ( int numWords )  throws DocumentExhaustedException  {

        if ( lookAheadBuffer.size() >= numWords) return ; // Already have enough words in the buffer

        for ( int i=1; i<=(numWords-lookAheadBuffer.size()) ; i++ ) {
            if ( scanner.hasNext() ) {
                lookAheadBuffer.add ( scanner.next()) ;
            } else {
                throw new DocumentExhaustedException("ScannableDocument.loadLookAheadBuffer()") ;
            }
        }
    }
/**
*返回文档中的下一个单词。只是扫描程序和前瞻缓冲区方法的包装器。
*调用方不必调用hasNext()。
*调用方必须捕获DocumentExhustedException。
*@从扫描仪返回下一个单词
*@depustedexception
*/
公共字符串getNextWord()引发DocumentExhustedException{
//如果lookAheadBuffer不为空,则返回其第一个元素(fifo队列),而不是移动扫描仪
如果(!lookAheadBuffer.isEmpty()){
返回lookAheadBuffer.removeFirst();
}否则{
if(scanner.hasNext()){
返回scanner.next();
}否则{
抛出新的DocumentExustedException(“ScannableDocument.getNextWorld()”;
}
}
}
/**
*将numWords移出输送机。
*@param numWords
*@depustedexception
*/
public void advanceConveyer(int numWords)抛出DocumentExhustedException{
for(int-word=1;word=numWords)return;//缓冲区中已有足够的字

对于(int i=1;我建议您阅读一次扫描仪。它的设计目的是与用户进行交互,我怀疑您是否希望在用户键入之前询问他们想要键入什么。我同意Peter的观点:扫描仪消耗输入流,如果它调用批量读取来填充自己的内容,它将永久提升底层流的状态外部缓冲区。我看不出这个想法(合理地)是如何工作的。可能是我没有明确我的目的。我正在从文档而不是流构建扫描仪。我需要查看当前扫描仪位置,但不更改原始扫描仪的状态,即不调用getNext()在原始扫描仪上。@user903724在某些情况下,扫描仪将不会有下一个值,那么应该发生什么?这实际上是本练习的目的。展望未来: