Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Scanner hasNextFloat() method in Java with Examples
Next article icon

Scanner hasNextBoolean() method in Java with Examples

Last Updated : 16 Oct, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The hasNextBoolean() method of java.util.Scanner class returns true if the next token in this scanner's input can be interpreted as a Boolean using the nextBoolean() method. The scanner does not advance past any input. Syntax:
public boolean hasNextBoolean()
Parameters: The function does not accepts any parameter. Return Value: This function returns true if and only if this scanner's next token is a valid Boolean. Exceptions: The function throws IllegalStateException if this scanner is closed. Below programs illustrate the above function: Program 1: Java
// Java program to illustrate the // hasNextBoolean() method of Scanner class in Java // without parameter  import java.util.*;  public class GFG1 {     public static void main(String[] argv)         throws Exception     {          String s = "gfg true geeks!";          // new scanner with the         // specified String Object         Scanner scanner = new Scanner(s);          // use US locale to interpret Booleans in a string         scanner.useLocale(Locale.US);          // iterate till end         while (scanner.hasNext()) {              // check if the scanner's             // next token is a Boolean with the default radix             System.out.print("" + scanner.hasNextBoolean());              // print what is scanned             System.out.print(" -> " + scanner.next() + "\n");         }          // close the scanner         scanner.close();     } } 
Output:
  false -> gfg  true -> true  false -> geeks!  
Program 2: Program to demonstrate exception Java
// Java program to illustrate the // hasNextBoolean() method of Scanner class in Java // Exception case  import java.util.*;  public class GFG1 {     public static void main(String[] argv)         throws Exception     {          try {             String s = "gfg 2 geeks!";              // new scanner with the             // specified String Object             Scanner scanner = new Scanner(s);              // use US locale to interpret Booleans in a string             scanner.useLocale(Locale.US);              scanner.close();              // iterate till end             while (scanner.hasNext()) {                  // check if the scanner's                 // next token is a Boolean with the default radix                 System.out.print("" + scanner.hasNextBoolean());                  // print what is scanned                 System.out.print(" -> " + scanner.next() + "\n");             }              // close the scanner             scanner.close();         }         catch (IllegalStateException e) {             System.out.println("Exception: " + e);         }     } } 
Output:
  Exception: java.lang.IllegalStateException: Scanner closed  
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextBoolean()

Next Article
Scanner hasNextFloat() method in Java with Examples
author
gopaldave
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Functions
  • Java-Scanner
Practice Tags :
  • Java

Similar Reads

  • Scanner hasNextDouble() method in Java with Examples
    The hasNextDouble() method of java.util.Scanner class returns true if the next token in this scanner's input can be interpreted as a Double using the nextDouble() method. The scanner does not advance past any input. Syntax: public Double hasNextDouble() Parameters: The function does not accepts any
    2 min read
  • Scanner hasNextByte() method in Java with Examples
    The hasNextByte() method of java.util.Scanner class returns true if the next token in this scanner's input can be assumed as a Byte value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be default radix
    3 min read
  • Scanner hasNextLine() method in Java with Examples
    The hasNextLine() method of java.util.Scanner class returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input. Syntax: public boolean hasNextLine() Parameters: The function does not accepts any param
    2 min read
  • Scanner hasNextLong() method in Java with Examples
    The hasNextLong() method of java.util.Scanner class returns true if the next token in this scanner's input can be assumed as a Long value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be default radix
    3 min read
  • Scanner hasNextFloat() method in Java with Examples
    The hasNextFloat() method of java.util.Scanner class returns true if the next token in this scanner's input can be interpreted as a Float using the nextFloat() method. The scanner does not advance past any input. Syntax: public Float hasNextFloat() Parameters: The function does not accepts any param
    2 min read
  • Scanner hasNextInt() method in Java with Examples
    The hasNextInt() method of java.util.Scanner class returns true if the next token in this scanner's input can be assumed as a Int value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be default radix an
    3 min read
  • Scanner hasNextBigDecimal() method in Java with Examples
    The hasNextBigDecimal() method of java.util.Scanner class returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method. The scanner does not advance past any input. Syntax: public boolean hasNextBigDecimal() Parameters: The function does
    2 min read
  • Scanner hasNextShort() method in Java with Examples
    The hasNextShort() method of java.util.Scanner class returns true if the next token in this scanner's input can be assumed as a short value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be default radi
    3 min read
  • Scanner hasNextBigInteger() method in Java with Examples
    The hasNextBigInteger() method of java.util.Scanner class returns true if the next token in this scanner's input can be assumed as a BigInteger value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be de
    3 min read
  • Scanner findInLine() method in Java with Examples
    findInLine(Pattern pattern)The findInLine(Pattern pattern) method of java.util.Scanner class tries to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences