Java Scanner hasNext() Method

The hasNext() method in Java's `Scanner` class is crucial for determining token availability in input. Its three variations allow developers to specify diverse conditions for token presence, enriching input validation and parsing capabilities. It enhances flexibility and precision in handling various input scenarios, and optimizing program functionality.

The method signature for the basic hasNext() is public boolean hasNext(), returning true if the scanner has another token in its input. Similarly, the overloads hasNext(Pattern pattern) and hasNext(String pattern) accommodate pattern matching against tokens, extending the method's versatility in input validation. These variations provide developers with precise control over input verification, ensuring efficient and accurate parsing in Java applications. These are:

  1. Java Scanner hasNext () Method
  2. Java Scanner hasNext (String pattern) Method
  3. Java Scanner hasNext(Pattern pattern) Method

1. Java Scanner hasNext () Method:

The hasNext() method in Java's Scanner class is used to determine whether the scanner has another token in its input. It returns true if there is at least one more token present in the input and false otherwise.

Signature:

Return Value:

true: Indicates that the scanner has another token in its input.

false: Indicates that there are no more tokens left in the input or the input stream is empty.

Behavior:

The method examines the next token in the input stream without consuming it.

It returns true if there is at least one more token present in the input stream, and false otherwise.

If the input stream is empty, or if there are no more tokens left to read, the method returns false.

The input stream remains unchanged after calling this method.

ScannerExample.java

Output:

Scanner has next token: true 

2. Java Scanner hasNext(String pattern) Method:

The hasNext(Pattern pattern) method in the Scanner class is used to check if the next complete token in the input matches the specified pattern. It returns true if the next token matches the specified regular expression pattern; otherwise, it returns false.

Signature:

Description: Returns true if the next complete token matches the specified pattern; otherwise, returns false.

Parameters:

pattern: A Pattern object representing the regular expression pattern to match against the next token in the input.

Return Value: true if the next complete token matches the specified pattern; otherwise, false.

Suppose we have a Scanner object initialized with the input "123 456" and we want to check if the next token is an integer. We can use hasNext(Pattern pattern) with a pattern that matches integers to perform this check.

ScannerExample.java

Output:

Scanner has next integer token: true

3. Java Scanner hasNext (Pattern pattern) Method:

The hasNext(Pattern pattern) method in Java's Scanner class allows us to check if the next token in the input matches a specific pattern defined by a regular expression pattern object (java.util.regex.Pattern). The method provides flexibility in determining whether the upcoming token adheres to a particular format or structure without consuming the input.

Signature:

Parameters:

pattern: A Pattern object representing the regular expression pattern to be matched against the next token in the input.

Return Value:

true: If the next complete token in the input stream matches the specified pattern.

false: If the next token in the input stream doesn't match the pattern or if the input stream has reached its end.

Behavior:

The method examines the next token in the input stream without advancing the scanner's position.

It returns true if the next token matches the specified pattern; otherwise, it returns false.

The input stream remains unchanged after calling this method.

Consider an example where we have a Scanner object initialized with the input "123abc456def", and we want to check if the next token consists of digits only:

ScannerExample.java

Output:

Scanner has next token containing digits: false