Home » Java programs » Java basic programs

Java program to convert string to Boolean



In this java program, we are going to learn how to convert a string value to Boolean value? Here, we are taking an input as string and converting it into Boolean value.
Submitted by IncludeHelp, on December 05, 2017

Given input as string and we have to convert it into Boolean value using java program.

Example:

Input string: ‘true’
Output:
Boolean value: true

Here,

  • First we are taking input as string using statement String str =KB.next();
  • And, then creating an object of "Boolean" class by passing string value to its constructor, statement: Boolean blnObj1 = new Boolean(str);
  • And, then printing the object blnObj1 using System.out.println
  • The output value will be Boolean value.

Program to convert string to Boolean in java

import java.util.Scanner;

public class ConvertStringToBoolean 
{
	public static void main(String[] args) 
	{
		// create object of scanner.
		Scanner KB=new Scanner(System.in);

		// enter path here.
		System.out.print("Enter the string here : ");

		//construct String object
		String str =KB.next();

		// Convert using constructor
		Boolean blnObj1 = new Boolean(str);
		System.out.println("Boolean value is : " +blnObj1);
	}  
}

Output

First run:
Enter the string here : true
Boolean value is : true

Second run:
Enter the string here : false
Boolean value is : false





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.