Java Scanner hasNext() MethodThe hasNext() is a method of Java Scanner class which returns true if this scanner has another token in its input. There are three different types of Java Scanner hasNext() method which can be differentiated depending on its parameter. These are:
1.Java Scanner hasNext () Method:It is a Scanner class method which returns true if this scanner has another token in its input. This method may block while waiting for input to scan. 2.Java Scanner hasNext (String pattern) Method:It is a Scanner class method which returns true if the next token matches the pattern constructed from the specified string. 3.Java Scanner hasNext (Pattern pattern) Method:It is a Scanner class method which returns true if the next complete token matches the specified pattern. SyntaxFollowing are the declarations of hasNext() method: Parameter
Returns
ExceptionsIllegalStateException- This method throws exception if the innvocation is done after the scanner has been closed. Compatibility VersionJava 1.5 and above Example 1Output: Delimiter:\p{javaWhitespace}+ Hello World! Example 2Output: Result: true String: Hello, This is JavaTpoint. Final Result: false Example 3Output: Pattern found Example 4Output: Result: true Result: false Rest of String: JavaTpoint.com 15 + 15 = 18.0 Example 5Output: Pawan Suresh
Next TopicJava Scanner Class
|