Home » Java programming language

Java Scanner hasNextBigInteger() Method with Example

Scanner Class hasNextBigInteger() method: Here, we are going to learn about the hasNextBigInteger() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on March 24, 2020

Scanner Class hasNextBigInteger() method

  • hasNextBigInteger() method is available in java.util package.
  • hasNextBigInteger() method is used to check whether the next token in this Scanner input can be manipulated as a BigInteger or not.
  • hasNextBigInteger() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • hasNextBigInteger() method may throw an exception at the time of checking BigInteger.
    IllegalStateException: This exception may throw when this Scanner is not opened.

Syntax:

    public boolean hasNextBigInteger();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is boolean, it returns true when this Scanner next token is a valid BigInteger using the nextBigInteger() method otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean hasNextBigInteger() method 
// of Scanner 

import java.util.*;
import java.util.regex.*;

public class HasNextBigInteger {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24";

  // Instantiates Scanner
  Scanner sc = new Scanner(str);

  // By using hasNextBigInteger() method is to
  // check whether this object next token
  // represents BigInteger or not

  boolean status = sc.hasNextBigInteger();
  System.out.println("sc.hasNext(): " + status);

  // By using hasNextBigInteger() method is to
  // check whether this object next token
  // represents BigInteger with the given radix
  // or not

  status = sc.hasNextBigInteger(2);
  System.out.println("sc.hasNext(2): " + status);

  // Scanner closed
  sc.close();
 }
}

Output

sc.hasNext(): false
sc.hasNext(2): false


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.