Scanner next() confusion

In class java.util.Scanner ,method public String next() finds and returns the next complete token from this Scanner ,I am confused,if I write a program like this:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println(in.next());
    in.close();
}

and run this program,then input a word and the word will be printed,it seems that method next() returns the current token,why does the API say next() returns the next complete token?


This is because you assigned the Scanner to standart input ( System.in ).

If you use your exact program in this

IDEONE DEMO

OUTPUT (check in the link how stdin is assigned prior to execute)

Success!

stdin  // this is what you assign
    hello word

stdout  // this is the output of main method
    hello
    word

If this does not clarify, maybe you will find this example useful. Check how I assign the Scanner to a created String :

String input = "hello my name is Jordi";
Scanner s = new Scanner(input);    // assign the scanner to String s!!!!
System.out.println(s.next());      // prints: hello
System.out.println(s.next());      // prints: my
System.out.println(s.next());      // prints: name
System.out.println(s.next());      // prints: is
System.out.println(s.next());      // prints: Jordi
s.close(); 

I will describe what all of this is about, since you are confused.

Scanner input = new Scanner(System.in) 

java.lang.System is a public final class extending java.lang.Object and it has static fields namely err and out being of type PrintStream and in being of type InputStream hence,

System.in

java.util.Scanner extends java.lang.Object and implements the following interfaces:

  • Iterator
  • Closeable
  • AutoCloseable
  • Now that we understood the hierarchy. What happens during execution?

    Execution of > Scanner input = new Scanner(System.in)
    

    Constructs a new Scanner object passing it the source through which it should expect the input.

    Execution of > input.next() 
    

    does the following steps

  • Block execution while waiting for input to scan
  • As soon as you provide an input (assume below)

    "Hello World! This is a test." 
    

    and hit Enter the following steps take place

  • Scanner read the data from Input Stream
  • Tokenizes the input using the delimiter (default whitespace)
  • Construct an iterator similiar to Iterator iterate = tokens.iterator() for iteration through tokens
  • Find the first complete token being "Hello" in the scanner, returns the token and waits before next token.
  • The reason the first complete token is returned is because that is how next() method that is inherited from java.util.Iterator behaves. Basically think of it a pointer pointing to bunch of tokens in scanner arranged in an order. As soon as next() is invoked, returns first token and moves the pointer ahead.

    hasNext() on the other hand, returns true if this scanner has another token from the location the iterator is pointing to. Unlike next() it does not advance past the token.

    The documentation says the following about next()

    Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.


    链接地址: http://www.djcxy.com/p/96090.html

    上一篇: 扫描仪类错误

    下一篇: 扫描仪next()混淆