Convert string to boolean java – Java Program to Convert String to Boolean

Convert string to boolean java: In the previous article we have discussed Java Program to Convert String to Object

In this article we will see how to convert String type to Boolean type.

Program To Convert string to Boolean

Java convert string to boolean: Before converting let’s see some example of both the types.

Example-1: Boolean type

boolean a = true;
boolean b = false;
Example-2: String type

String a = "true"; 
String b = "BtechGeeks";

Let’s see different ways to do it.

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Method 1 : Java Program to Convert String to Boolean Using parseInt() method

How to convert string to boolean in java: string type variable can be converted to Boolean by using  parseInt() see how it will work.

Here this method is  a wrapper class in Java. This method of the Boolean class   converts the string variables into Boolean.

Approach :

  1. Take a string value and store it in a string variable input1.
  2. Then pass that input1 variable as parameter to parseInt() method which will convert the string into Boolean value and return it .
  3. Store that Boolean value in a variable output .
  4. Display the result .

Program:

import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a character through scanner class
        System.out.print("Enter a string : ");
        String input1=sc.next();
        // converting to Boolean
        boolean output =Boolean.parseBoolean(input1) ;
        System.out.println("Converted Boolean value is : " + output);
    }
}
Output : 

Enter a string : true
Converted Boolean value is : true

Method 2 : Java Program to Convert String to Boolean Using valueOf() method

String type variable can be converted to Boolean by using  valueOf() , let’s see how it actually works.

This method returns an object of the Boolean class. However, the object will be  automatically converted into the primitive type.

 Approach :

  1. Take a string value and store it in a string variable input1.
  2. Then pass that input1 variable as parameter to valueOf() method which will convert the String into Boolean value and return it .
  3. Store that Boolean value  in a variable output .
  4. Display the result .

Program:

import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a character through scanner class
        System.out.print("Enter a string : ");
        String input1=sc.next();
        // converting to Boolean
        boolean output =Boolean.valueOf(input1) ;
        System.out.println("Converted Boolean value is : " + output);
    }
}
Output : 

Enter a string : true
Converted Boolean value is : true

Guys who are serious about learning the concepts of the java programming language should
practice this list of programs in java and get a good grip on it for better results in exams or
interviews.

Related Java Program: