How To Convert String To Boolean in Java

The Tutorial we are going to learn is How to Convert String to Boolean in Java. Let us know about both data types before we start: The Boolean data type is a basic data type in Java, which means it comes pre-installed with the language. Only true or false can be the value of a Boolean data type. These are reserved terms in the programming language and hence they cannot be used to identify variables, functions, classes, or objects! A switch, in this example true or false, is a fundamental idea in every programming language. As a result, the outcome of a Boolean operation is either true or false (lowercase in Java code). Other Java programming capabilities rely on a Boolean value intrinsically, which may not be clear at first.

In computer programming, a string is a sequence of characters that is used either as a constant or as a variable. The latter may allow for the mutation of its elements and the modification of their length, or it may be fixed (after creation). In computing, a string is a data type that is frequently implemented as an array of bytes (or words) that stores a succession of items, most commonly characters, using some character encoding.

 

What’s the Approach?

 

We can solve this problem in 2 ways one by using Boolean.ParseBoolean() and the other Boolean.valueOf().

 

  • Import ‘java.util’ package to the class

 

  • Declare the main function

 

  • Create a Scanner Class

 

  • Declare a String variable and accept input from the user.

 

  • Create a Boolean variable and use the function Boolean.ParseBoolean()

Or

  • Create a Boolean variable and use the function Boolean.valueOf().

 

Also Read:-  How to convert Int to Double in Java

 

Java Program to Convert String to Boolean

 

Method 1:-

/*

 * TechDecode Tutorials

 *

 * How to Convert String to Boolean

 *

 */

import java.util.*;

public class String_to_Boolean

{

    public static void main(String args[])

    {

        //Declaring Scanner Class

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter the String 'True or False'");

        //Creating String and Accpeting value

        String s=sc.nextLine();

        //Converting

        boolean b=Boolean.parseBoolean(s);

        //Printing value of boolean

        System.out.println("Boolean = "+b);

    }

}


 

Method 2: 

/*

 * TechDecode Tutorials

 *

 * How to Convert String to Boolean

 *

 */

import java.util.*;

public class String_to_Boolean

{

    public static void main(String args[])

    {

        //Declaring Scanner Class

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter the String 'True or False'");

        //Creating String and Accpeting value

        String s=sc.nextLine();

        //Converting

        boolean b=Boolean.valueOf(s);

        //Printing value of boolean

        System.out.println("Boolean = "+b);

    }

}

 

Output:-

Output Of the Program

Leave a Reply

Your email address will not be published. Required fields are marked *