Scanner hasNextBigDecimal() in Java

hasNextBigDecimal(): This method is available in java.util.Scanner class of Java.

Syntax:

boolean java.util.Scanner.hasNextBigDecimal()

This method returns true if the next token in this scanner's input can be interpreted as a BigDecimal.

Parameters: NA

Returns: true if and only if this scanner's next token is a valid BigDecimal.

Throws:

IllegalStateException - if this scanner is closed

Approach 1: When no exception

Java

import java.util.Scanner;

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

        String source = "Hello World";
        Scanner scanner = new Scanner(source);

        System.out.println(scanner.hasNextBigDecimal());

        scanner.close();
    }
}

Output:

false


Approach 2: IllegalStateException 

Java

import java.util.Scanner;

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

        String source = "Hello World";
        Scanner scanner = new Scanner(source);

        scanner.close();
        System.out.println(scanner.hasNextBigDecimal());

    }
}

Output:

Exception in thread "main" java.lang.IllegalStateException: Scanner closed at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150) at java.base/java.util.Scanner.hasNext(Scanner.java:1540) at java.base/java.util.Scanner.hasNextBigDecimal(Scanner.java:2699)


No comments:

Post a Comment