Java Utility Library

Java Scanner - hasNextBigInteger() Method



The java.util.Scanner.hasNextBigInteger() method returns true if the next token in the scanner's input can be interpreted as a BigInteger value in the specified radix using the nextBigInteger() method. The scanner does not advance past any input.

Syntax

public boolean hasNextBigInteger(int radix)

Parameters

radix Specify the radix used to interpret the token.

Return Value

Returns true if and only if the scanner's next token is a valid BigInteger value.

Exception

Throws IllegalStateException, if the scanner is closed.

Example:

In the example below, the java.util.Scanner.hasNextBigInteger() method is used to check whether the scanner's next token is a valid BigInteger value or not in the specified radix.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {

    //String to scan
    String MyString = "Hello World 10 + 20 = 30.0";

    //creating a Scanner
    Scanner MyScan = new Scanner(MyString);

    while(MyScan.hasNext()) {
      //check if the next token is a BigInteger with radix 16
      //if yes, prints BigInteger value
      if(MyScan.hasNextBigInteger(16))
        System.out.println("BigInteger value is: "+ MyScan.nextBigInteger(16));
      //if the next token is not a BigInteger
      else
        System.out.println("No BigInteger Value found: "+ MyScan.next());
    }

    //close the scanner
    MyScan.close();
  }
}

The output of the above code will be:

No BigInteger Value found: Hello
No BigInteger Value found: World
BigInteger value is: 16
No BigInteger Value found: +
BigInteger value is: 32
No BigInteger Value found: =
No BigInteger Value found: 30.0

❮ Java.util - Scanner