Java Scanner next() MethodThe next() is a method of Java Scanner class which finds and returns the next complete token from the scanner which is in using. There are three different types of Java Scanner next() method which can be differentiated depending on its parameter. These are:
1. Java Scanner next() MethodIt is a Scanner class method used to get the next complete token from the scanner which is in using. A complete token is preceded and followed by input that matches the delimiter pattern. 2. Java Scanner next(String pattern) MethodIt is a Scanner class method which returns the next token if it matches the pattern constructed from the specified string. 3. Java Scanner next(Pattern pattern) MethodIt is a Scanner class method which returns the next token if it matches the specified pattern. SyntaxFollowing are the declarations of next() method: Parameter
ReturnsThe next() method returns the next complete tokens. ExceptionsNoSuchElementException- It will thrown this Exception if no more tokens are found. IllegalStateException- It will thrown this Exception if the innvocation is done after Scanner is closed. Compatibility VersionJava 1.5 and above Example 1Output: Enter full name: Hritik Roshan First Name is: Hritik Last Name is: Roshan Example 2Output: hasNextLine public boolean hasNextLine() IllegalStateException Example 3Output: Token Value1 Facebook.com Token value2: JavaTpoint.com Example 4Output: tokenized Strings: 22 tokenized Strings: 313 tokenized Strings: 45 tokenized Strings: 87 Example 5Output: JavaTpoint Hello Next TopicJava-scanner-nextshort-method |