Home » Java programming language

Java Scanner nextBoolean() Method with Example

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

Scanner Class nextBoolean() method

  • nextBoolean() method is available in java.util package.
  • nextBoolean() method is used to scans the next token of the input scanned into a boolean value.
  • nextBoolean() 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.
  • nextBoolean() method may throw an exception at the time of returning a Boolean value.
    • InputMismatchException: This exception may throw when the next token input mismatch.
    • NoSuchElementException: This exception may throw when no such element exists.
    • IllegalStateException: This exception may throw when this Scanner is not opened.


    public boolean nextBoolean();


  • It does not accept any parameter.

Return value:

The return type of the method is boolean, it returns true when the valid boolean value scanned from the input otherwise it returns false.


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

import java.util.*;

public class NextBooleanOfScanner {
    public static void main(String[] args) {
        String str = "Hi, true IncludeHelp! 8 + 2.0 = 10.0";

        // Instantiate Scanner with the 
        // given str
        Scanner sc = new Scanner(str);

        // Loop for scanning the boolean
        // token input
        while (sc.hasNext()) {
            // if boolean then display it
            if (sc.hasNextBoolean()) {
                System.out.println("Boolean Exists: " + sc.nextBoolean());

        // close the scanner


Boolean Exists: true

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.