devxlogo

Finding a Boolean that Is Part of an Input String

Ever wanted to find a boolean that is available as part of the input string? The following code snippet helps you do it quickly.

import java.util.*;public class ScannerUtils {   public static void main(String args[])   {      ScannerUtils scannerUtils = new ScannerUtils();      scannerUtils.proceed();   }   final String stringSentence = "Hi there, is it true that you are coming home today?";      private void proceed()   {      //Creating a Scanner with the string stringSentence      Scanner scanner = new Scanner(stringSentence);      //Scanning the string       while (scanner.hasNext()) {         //Checking if the next scanned section is a boolean         System.out.println(scanner.hasNextBoolean() + " : " + scanner.next().toString());      }      //Closing the Scanner      scanner.close();   }}/*Expected output:[root@mypc]# java ScannerUtils false : Hifalse : there,false : isfalse : ittrue : truefalse : thatfalse : youfalse : arefalse : comingfalse : homefalse : today?*/

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.