Home » Java programming language

Java Scanner nextBigDecimal() Method with Example

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

Scanner Class nextBigDecimal() method

  • nextBigDecimal() method is available in java.util package.
  • nextBigDecimal() method is used to get the big decimal scanned from the input.
  • nextBigDecimal() 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.
  • nextBigDecimal() method may throw an exception at the time of returning BigDecimal.
    • InputMismatchException: This exception may throw when the next token input mismatch or not.
    • NoSuchElementException: This exception may throw when no such element exists.
    • IllegalStateException: This exception may throw when this Scanner is not opened.

Syntax:

    public BigDecimal nextBigDecimal();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is BigDecimal, it returns the BigDecimal scanned from the input.

Example:

// Java program to demonstrate the example 
// of BigDecimal nextBigDecimal() method of Scanner 

import java.util.*;

public class NextBigDecimalOfScanner {
    public static void main(String[] args) {
        String str = "Hi, [IncludeHelp] +\n 10.0 true ";

        // Instantiates a Scanner object with
        // the given string str
        Scanner sc = new Scanner(str);

        // Display str
        System.out.println("sc.nextLine(): " + sc.nextLine());

        // By using nextBigDecimal() method is
        // to return the BigDecimal if exists
        System.out.println("sc.nextBigDecimal(): " + sc.nextBigDecimal());

        // By using close() method is to 
        // close the Scanner object
        sc.close();
    }
}

Output

sc.nextLine(): Hi, [IncludeHelp] +
sc.nextBigDecimal(): 10.0



Comments and Discussions!

Load comments ↻






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