Register Login

Java.util.Scanner.hasNext() Method

Updated Jan 08, 2020

What is hasNext() method in Java?

The hasNext() is a method of  java.util.Scanner class, which returns true if this scanner has another token in its input.

The hasNext() method can be distinguished on the basis of the parameter passed such as

1) java.util.Scanner hasNext () //No Parameter passed

This java.util.Scanner class method returns true if this scanner has another token in its input. It may block waiting for input to scan because the scanner does not advance past any input.

2) java.util.Scanner hasNext (String_Pattern) //String pattern Parameter passed

This java.util.Scanner class method returns true if the next token matches the pattern of a specified string

3) java.util.Scanner hasNext (Pattern_Pattern) //Pattern pattern Parameter passed

This java.util.Scanner class method returns true if the next token matches the specified pattern.

Syntax

For No parameter 

public boolean hasNext()  

For String Pattern Parameter 

public boolean hasNext(String_Pattern)  

For Pattern pattern Parameter 

public boolean hasNext(Pattern_Pattern)  

Parameters:

Parameter Type Description
String_Pattern String string containing the pattern to be scanned
Pattern_Pattern Pattern pattern to be scanned for any specified string

Return Value

The Java.util.Scanner.hasNext() Method method return TRUE if the scanner has another token in its input.

Exception

The hasNext() function returns exception IllegalStateException if in case the scanner is closed

Program to Demonstrate exception IllegalStateException

//Java program to illustrate the IllegalStateException in hasNext Function

//Importing the Scanner Class of Util Package
import java.util.*;

//Main Class of the program
public class Main {
    //Main method of the program
    public static void main(String args[]){
        //String Variable
        String str = "STechies - Online Tutorials";
        //Scanner Class object
        Scanner scanner = new Scanner(str);
        //Checking scanner's next token matches the String pattern
        //Scanner object closed before innovation
        scanner.close();
        System.out.println("Match Found : "+scanner.hasNext("Stechies"));
    }
} 

OUTPUT

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Scanner.java:1070)
    at java.util.Scanner.hasNext(Scanner.java:1433)
    at java.util.Scanner.hasNext(Scanner.java:1400)
    at Main.main(Main.java:11)

Example of  hasNext() in Java

Program 1: java.util.Scanner hasNext ()

//Java code for hasNew Function

//Importing the Scanner Class of Util Package
import java.util.Scanner;

//Main Class of the program
public class Main {
    //Main Method of the program
    public static void main (String[] args) {
        //String variable
        String s = "STechies - Online Tutorials";
        //Creating the object of Scanner Class
        Scanner scan = new Scanner(s);
        //Printing the output
        System.out.println(scan.hasNext());
    }
} 


OUTPUT:

true 

Program 2: java.util.Scanner hasNext (String_Pattern)

//Java code for hasNew Function with pattern

//Importing the Scanner Class of Util Package
import java.util.Scanner;

//Main Class of the program
public class Main {
    //Main Method of the program
    public static void main (String[] args) {
        //String variable
        String string = "STechies - Online Tutorials";

        //Creating the object of Scanner Class
        Scanner scan = new Scanner(string);

        String pattern = "STechies.*";

        if(scan.hasNext(pattern)){
            System.out.println("pattern matched");
        }else{
            System.out.println("Unmatched");
        }
    }
} 

OUTPUT:

pattern matched

Program 3: java.util.Scanner hasNext (Pattern_Pattern)

//Java program to illustrate the working of hasNext String pattern
//Importing the Scanner Class of Util Package
import java.util.*;
//Main Class of the program
public class Main {
    //Main Method of the program
    public static void main(String args[]){
        //String Variable
        String str = "STechies - Online Tutorials";
        //Scanner Class object
        Scanner scanner = new Scanner(str);
        //Checking scanner's next token matches the String pattern
        System.out.println("Match Found : "+scanner.hasNext("Stechies"));
    }
} 

OUTPUT :

Match Found : false 


×