javajava.util.scanner

Java's new Scanner vs Scanner.create()?


I was looking through O'Reillys Java Cookbook (2ed) for some good stuff and found Scanner.create() method about 10 times. But there's no such in the API or class declaration\implementation. Ex: Page example


Solution

  • It's either referring to a non-SDK Scanner type or is an error in the book.

    There is no static Scanner Scanner.create() in Java 1.5/5 (when it was introduced), or in the SDK 6 or SDK 7 APIs. There is also no mention of such a method being obsoleted (which, in Java SDK API, effectively means it never [officially] existed :-).

    Update note: It appears that the create factory method did indeed exist in the earliest preview/beta versions of Java 5. RanRag found a relevant thread on the issue:

    ...and no, you aren't going crazy: Scanner had create() methods in [Java 5] tiger-beta1, but they switched to constructors in [Java 5] beta2.

    (So the only correct way to is use the constructor.)

    Happy coding.