Java Scanner hasNext() Method
Overview
The scanner class contains the hasNext() method that is used to check if the unread input exists. It returns true if input is else, it returns false. Unline next() methods of the Class it doesn't throw a NullPointer exception; instead, it simply returns a boolean value. It can match the tokens with a string or a pattern.
Syntax of Java Scanner hasNext() Method
The hasNext() method in Java has three methods as follows:
The first constructor is the default one; it simply checks if the token is available or not.
The second constructor method takes a String as an input. If the next token matches the pattern, then it returns true. Else, it returns false.
If the Scanner class method matches the given pattern object of the Pattern class, it returns a boolean value based on it.
Parameter of Java Scanner hasNext() Method
Parameters are optional in this method. Out of the three methods available, one takes no input and simply checks if the next token is available or not.
The other two methods take a single parameter, which is either a String or an object of the class Pattern. This String or Pattern matches the next available token.
Return Values of Java Scanner hasNext() Method
The first constructor returns true if the next token is available. Else, it returns false.
The other two methods match the next token with the String or the Pattern. If it matches with the pattern, then it returns true. Else, it returns false.
Explanations
The Scanner class in Java reads user input. This input can be fixed or variable. It is sometimes not feasible to know the length of the input in advance. So, the question arises: How long do we keep reading? Also, the scanner.next() method throws a NullPointerException if we try to read empty input.
Thus, Java provides a method called hasNext() that returns a boolean instead of throwing an error. If the unread input is present, it returns true. Else, it returns false.
This is true for all the next methods, i.e. nextInt(), nextFloat(), nextLine(), etc.
Compatibility Version
The hasNext() method is only compatible with Java version 1.5 and above.
Exceptions of Java hasNext() Method
It throws one exception IllegalStateException when the Scanner class is closed.
Examples of Java Scanner hasNext() Method
Example 1
Let us take an example better to understand the hasNext() method. Let us ask the user about their favorite books and add them to our database. We are not aware of the number of favorite books the user will enter.
One way is to ask them the number of inputs and then run a loop, but the user experience is not very good with this method of a pre-defined number of inputs. The hasNext() method allows us to dynamically check if we have any more inputs in the input window.
Code
Output
Explanation
We are using the Scanner class to read user input. Users can enter any number of books separated by the next line. We use the hasNext() method to check if we have any inputs left in the console to be read. If it returns true, we read the next input; otherwise, we break from the loop.
Example 2
Let us see another example of the hasNext() method where we use String matching, a built-in input parameter for the hasNext() method in Java. Here, we will take user input until the user chooses to Quit using #.
Code
Output
Explanation
In the above example, we ask the user to enter some input string. Here we are reading a string until we find #. # in the string denotes that the user is done entering input, and whatever is entered after hash is of no use. Lastly, we simply print the user Input.
Example 3
For instance, we have a newsletter subscription service, and users can provide their email input to subscribe to our newsletter.
To determine whether an email is valid, we will use Pattern Matching in the hasNext() method. If the user enters a valid email, we will add it to our database.
Regex (Regular Expression) for email matching is as follows:
- It accepts any number of characters before @. These characters can be numbers, lowercase and uppercase alphabets, and some special characters.
- Next, it accepts lowercase and uppercase letters, numbers, ., and -, which can be the provider's or the company's domain name.
- Lastly, we have a . after which TLD (top-level domain name) is matched. We are ensuring that its minimum length is 2.
Code
Output
Explanation
The above code asks the user to enter their email address to subscribe to the newsletter. The user's input is checked against REGEX, i.e., an object of the class Pattern in Java. If it matches the pattern of the email, then it is valid input; if it is not valid, then the program ends with an error. This ensures that the user enters the correct email address, and our database has legitimate user email IDs.
Conclusion
- The hasNext() method is used with the Scanner class to check if tokens are present in the input to be read.
- We can call the hasNext() method three different ways.
- It either takes no parameter or a String Object or Pattern Object as a parameter. It returns a boolean; if it finds a match, then it returns true. Else, it returns false.