How to check if String value is Boolean type in java?


The Boolean class of the lang package provides two method namely parseBoolean() and valueOf().

  • parseBoolean(String s) − This method accepts a String variable and returns boolean. If the given string value is "true" (irrespective of its case) this method returns true else, if it is null or, false or, any other value it returns false.

  • valueOf(String s) − This method accepts a String value, parses it and returns an object of the Boolean class based on the given value. You can use this method instead of the constructor. If the given String value is "true" this method returns true or, it returns false.

Example

 Live Demo

import java.util.Scanner;
public class VerifyBoolean {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string value: ");
      String str = sc.next();
      boolean result = Boolean.parseBoolean(str);
      System.out.println(result);
      boolean result2 = Boolean.valueOf(str);
      System.out.println(result2);
   }
}

Output1

Enter a string value:
true
true
true

Output2

Enter a string value:
false
false
false

But, neither of these methods verify whether the value of the given string is “true”. There is no method available to verify whether the value of a string is of boolean type. You need to directly verify using if loop or, regular expression.

Example : Using if loop

 Live Demo

import java.util.Scanner;
public class VerifyBoolean {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string value: ");
      String str = sc.next(); if(str.equalsIgnoreCase("true")||str.equalsIgnoreCase("false")){
      System.out.println("Given string is a boolean type");
      }else {
         System.out.println("Given string is not a boolean type");
      }
   }
}

Output1

Enter a string value:
true
Given string is a boolean type

Output2

Enter a string value:
false
Given string is a boolean type

Output3

Enter a string value:
hello
Given string is not a boolean type

Example : Using regular expression

 Live Demo

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VerifyBoolean {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string value: ");
      String str = sc.next();
      Pattern pattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(str);
      if(matcher.matches()) {
         System.out.println("Given string is a boolean type");
      } else {
         System.out.println("Given string is not a boolean type");
      }
   }
}

Output1

Enter a string value:
true
Given string is a boolean type

Output2

Enter a string value:
false
Given string is a boolean type

Output3

Enter a string value:
hello
Given string is not a boolean type

Updated on: 11-Oct-2019

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements