Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Intermediate
Jan 9, 2018



Application Security Testing: An Integral Part of DevOps

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();

   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

Expected output:

[root@mypc]# java ScannerUtils 
false : Hi
false : there,
false : is
false : it
true : true
false : that
false : you
false : are
false : coming
false : home
false : today?

M S Sridhar
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date