Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 29-07-2009
Member
 
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.
Reply With Quote
  #2  
Old 29-07-2009
Member
 
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
Reply With Quote
  #3  
Old 29-07-2009
Member
 
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>
Reply With Quote
  #4  
Old 29-07-2009
Member
 
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();
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "how to convert a boolean value to a string in java?"
Thread Thread Starter Forum Replies Last Post
Converting Boolean values to string in Java 27lynx27 Software Development 1 13-02-2011 03:40 AM
Convert string to binary in java TechGate Software Development 5 28-01-2010 01:36 PM
How to convert InputStream to String in java? KALIDA Software Development 4 20-01-2010 06:16 PM
How to convert String to Date object in java? KALANI84 Software Development 4 20-01-2010 05:07 PM
How to convert string into int in Java Zool Software Development 3 09-11-2009 12:41 PM


All times are GMT +5.5. The time now is 03:27 PM.