Convert String to Boolean value - Java java.lang

Java examples for java.lang:boolean

Description

Convert String to Boolean value

Demo Code

public class Main {

  public static void main(String[] argv) {
    String b = "java2s.com";
    System.out.println(toBool(b));

    b = "false";//from  www  . ja v  a  2 s  .c o  m
    System.out.println(toBool(b));

    b = "true";
    System.out.println(toBool(b));

    b = "yes";
    System.out.println(toBool(b));
  }

  public static boolean toBool(String b) {
    try {
      return Boolean.parseBoolean(b);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

}

Related Tutorials