dcsimg
 

Submitted by: M S Sridhar(sridhar.ms@gmail.com)

Tuesday Jan 9th 2018 by M S Sridhar

Language: Java, Level: Intermediate: - See how to find a boolean that is available as part of the 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 : Hi
false : there,
false : is
false : it
true : true
false : that
false : you
false : are
false : coming
false : home
false : today?

*/
Home
Mobile Site | Full Site