- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
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
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
- Related Articles
- How to check if String is empty in Java?
- How to check if type of a variable is string in Python?
- How to check if a variable is boolean in JavaScript?
- Boolean Type in Java
- How to convert a boolean value to string value in JavaScript?
- Java Program to convert boolean value to Boolean
- Java program to check if string is pangram
- How to check if a string is a valid keyword in Java?
- Java Program to Check if a String is Numeric
- C++ Program to Convert String Type Variables into Boolean
- Golang Program to convert string type variables into Boolean
- How to convert string type value to array type in JavaScript?
- PHP 8 – How to use ValueError to check if the value encountered is of correct type?
- Java Program to Check if two of three Boolean variables are true
- How to check if a string contains only one type of character in R?
