String to boolean Conversion in Java


String to boolean Conversion: Sometimes, we obtain the values in string format in Java. Printing the value is no problem (as long as user gets the same output) as it prints the same value as data type prints.

For example, a boolean value is obtained in string format as in command-line arguments or from TextField's getText() method. The string value is to be converted into boolean data type format to use in coding. To convert, casting does not work as string and boolean are incompatable for conversion either implicitly or explicitly. It requires extra effort in coding known as "parsing operation". Parsing operation involves the usage of a wrapper class and parseXXX() method. For string to boolean conversion, it is required Boolean class and parseBoolean() method and explained in the following program.

Parsing Example on String to boolean conversion
public class Conversions
{
  public static void main(String args[])
  {
    String str = "true";
    System.out.println("true in String form: " = + str);     // printing is no problem

    boolean b1 = Boolean.parseBoolean(str);                  // String to boolean conversion
          
    if(b1)
    {
      System.out.println("Yes converted");                   // using in coding
    }
  }
}


String to boolean
Output screenshot on String to boolean Example

parseBoolean() is a method of wrapper class Boolean which converts string str to boolean b1. Now b1 can be used in coding as in control structures.

Using the same technique, it is possible to convert string to other data types byte, short, int, long, float, double and character also.

Note: The other way boolean to string is also possible.

View all for 65 types of Conversions

2 thoughts on “String to boolean Conversion in Java”

  1. I am taking a Java class and in My Programming Lab the question is:

    Assume that an int variable age has been declared and already given a value. Assume further that the user has just been presented with the following menu:
    S: hangar steak, red potatoes, asparagus
    T: whole trout, long rice, brussel sprouts
    B: cheddar cheeseburger, steak fries, cole slaw
    (Yes, this menu really IS a menu!)
    Write some code that reads the String (S or T or B) that the user types in into a String variable choice that has already been declared and prints out a recommended accompanying drink as follows: if the value of age is 21 or lower, the recommendation is “vegetable juice” for steak, “cranberry juice” for trout, and “soda” for the burger. Otherwise, the recommendations are “cabernet”, “chardonnay”, and “IPA” for steak, trout, and burger respectively. Regardless of the value of age , your code should print “invalid menu selection” if the character read into choice was not S or T or B.

    ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input.

    My answer was:

    choice = stdin.next();
    if (choice = “S”){
    if (age <= 21)
    System.out.println("vegetable juice");
    System.out.println("cabernet");}
    else if (choice = "T"){
    if (age <= 21)
    System.out.println("cranberry juice");
    System.out.println("chardonnay");}
    else if (choice = "B"){
    if (age <= 21)
    System.out.println("soda");
    System.out.println("IPA");}
    else
    System.out.println("invalid menu selection");

    I keep getting a compilation error, stating that:

    CTest.java:11: error: incompatible types
    if (choice = "S"){
    ^
    required: boolean
    found: String
    CTest.java:15: error: incompatible types
    else if (choice = "T"){
    ^
    required: boolean
    found: String
    CTest.java:19: error: incompatible types
    else if (choice = "B"){
    ^
    required: boolean
    found: String
    3 errors

    Please Help!!!

Leave a Comment

Your email address will not be published.