Results 1 to 4 of 4

Thread: how to convert a boolean value to a string in java?

  1. #1
    Join Date
    May 2008
    Posts
    21

    how to convert a boolean value to a string in java?

    This is actually being a problem for me.
    I am working with my java code & I am not sure how to convert a boolean value to a string?

    I need some help in java converting boolean to string.

  2. #2
    Join Date
    May 2008
    Posts
    24

    Re: how to convert a boolean value to a string in java?

    Convert Boolean to String

    Code:
    public class Main {
        
           
        public static void main(String[] args) {
            boolean theValue = true;
            
            //boolean to String conversion
            String theValueAsString = new Boolean(theValue).toString();
            
            System.out.println(theValueAsString);
        }
    }
    //true

  3. #3
    Join Date
    May 2008
    Posts
    44

    Re: how to convert a boolean value to a string in java?

    Convert Boolean to String

    This program helps you in converting the Boolean type data into a string. The toString() method reads the Boolean type object and it converts into a string format. This method returns a string object and represents the boolean’s value. The Boolean object represents either “true” or “false".

    code

    Code:
    import java.util.*;
    
    public class BooleanToString{
      public static void main(String[] args){
        boolean b = true;
        String s = new Boolean(b).toString();
        System.out.println("String is:"+ s);
      }
    }
    Output

    Code:
    C:\corejava>java BooleanToString
    String is:true
    C:\corejava>

  4. #4
    Join Date
    May 2008
    Posts
    32

    Re: how to convert a boolean value to a string in java?

    Convert String to Boolean

    Try this for reverse conversion from string data type to boolean.
    There are 2 ways to do so.
    1. you can create an instance of the wrapper class Boolean with a string value as argument to the constructor and call the method booleanValue()
    2. call the static method parseBoolean() of the wrapper class to convert the string value to a boolean.
    Note : The string value sent in has to be either 'true' or 'TRUE' to result in a boolean with the value of true, but it doesn't have to be 'false' to result in a false boolean value.
    It actually makes sense that any other string value than 'true' or 'TRUE' will result in a return value that is set to false.

    Code:
    /**
     * Main.java
     *
     * @author www.javadb.com
     */
    public class Main {
        
        /**
         * String to boolean conversion
         */
        public void convertStringToBoolean() {
            
            String strBoolean = "true";
            
            //Do the String to boolean conversion
            boolean theValue = Boolean.parseBoolean(strBoolean);
            
            System.out.println(theValue);
        }
        
        /**
         * Starts the program
         *
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            new Main().convertStringToBoolean();
        }
    }

Similar Threads

  1. Converting Boolean values to string in Java
    By 27lynx27 in forum Software Development
    Replies: 1
    Last Post: 13-02-2011, 03:40 AM
  2. Convert string to binary in java
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 01:36 PM
  3. How to convert InputStream to String in java?
    By KALIDA in forum Software Development
    Replies: 4
    Last Post: 20-01-2010, 06:16 PM
  4. How to convert String to Date object in java?
    By KALANI84 in forum Software Development
    Replies: 4
    Last Post: 20-01-2010, 05:07 PM
  5. How to convert string into int in Java
    By Zool in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 12:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,472,103.92491 seconds with 16 queries