×

Java Programs

Java Practice

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

Problem statement

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

Example

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

Logic

  • 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

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.