The hasNextBigInteger() method of java.util.Scanner class returns true if the next token in this scanner’s input can be assumed as a BigInteger value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be default radix and functions accordingly. Syntax:Â
public boolean hasNextBigInteger(int radix) or public boolean hasNextBigInteger()
Parameters: The function accepts a single parameter radix which is not a mandatory one. It specifies the radix used to interpret the token as a BigInteger value. Return Value: This function returns true if and only if this scanner’s next token is a valid BigInteger value in the default radix. Exceptions: The function throws IllegalStateException if this scanner is closed. Below programs illustrate the above function: Program 1:Â
Java
// Java program to illustrate the // hasNextBigInteger() method of Scanner class in Java // with parameter Â
import java.util.*; Â
public class GFG1 {     public static void main(String[] argv)         throws Exception     { Â
        String s = "gfg 2 geeks!"; Â
        // new scanner with the         // specified String Object         Scanner scanner = new Scanner(s); Â
        // use US locale to interpret BigIntegers in a string         scanner.useLocale(Locale.US); Â
        // iterate till end         while (scanner.hasNext()) { Â
            // check if the scanner's             // next token is a BigInteger with a radix 3             System.out.print("" + scanner.hasNextBigInteger( 3 )); Â
            // print what is scanned             System.out.print(" -> " + scanner.next() + "\n");         } Â
        // close the scanner         scanner.close();     } } |
false -> gfg true -> 2 false -> geeks!
Program 2:Â
Java
// Java program to illustrate the // hasNextBigInteger() method of Scanner class in Java // without parameter Â
import java.util.*; Â
public class GFG1 {     public static void main(String[] argv)         throws Exception     { Â
        String s = "gfg 2 geeks!"; Â
        // new scanner with the         // specified String Object         Scanner scanner = new Scanner(s); Â
        // use US locale to interpret BigIntegers in a string         scanner.useLocale(Locale.US); Â
        // iterate till end         while (scanner.hasNext()) { Â
            // check if the scanner's             // next token is a BigInteger with the default radix             System.out.print("" + scanner.hasNextBigInteger()); Â
            // print what is scanned             System.out.print(" -> " + scanner.next() + "\n");         } Â
        // close the scanner         scanner.close();     } } |
false -> gfg true -> 2 false -> geeks!
Program 3: Program to demonstrate exceptionÂ
Java
// Java program to illustrate the // hasNextBigInteger() method of Scanner class in Java // Exception case Â
import java.util.*; Â
public class GFG1 {     public static void main(String[] argv)         throws Exception     { Â
        try {             String s = "gfg 2 geeks!"; Â
            // new scanner with the             // specified String Object             Scanner scanner = new Scanner(s); Â
            // use US locale to interpret BigIntegers in a string             scanner.useLocale(Locale.US); Â
            scanner.close(); Â
            // iterate till end             while (scanner.hasNext()) { Â
                // check if the scanner's                 // next token is a BigInteger with the default radix                 System.out.print("" + scanner.hasNextBigInteger()); Â
                // print what is scanned                 System.out.print(" -> " + scanner.next() + "\n");             } Â
            // close the scanner             scanner.close();         }         catch (IllegalStateException e) {             System.out.println("Exception: " + e);         }     } } |
Exception: java.lang.IllegalStateException: Scanner closed
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextBigInteger()