Java Boolean From toBoolean(String value)

Here you can find the source of toBoolean(String value)

Description

Converts the specified value string to the Boolean value.

License

BSD License

Parameter

Parameter Description
value a value string

Exception

Parameter Description
IllegalArgumentException if format error has occured

Return

a Boolean object

Declaration

static Boolean toBoolean(String value) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: BSD License 

public class Main {
    /**// w  ww  . j av  a 2 s.co m
     * Converts the specified value string to the Boolean value.
     * If the specified string is null, blank or 'false', this method returns false.
     * If it is "true", this method returns true. These comparation is case insensitive.
     * Otherwise, this method returns null.
     * 
     * @param value a value string
     * @return a {@code Boolean} object
     * @throws IllegalArgumentException if format error has occured
     */
    static Boolean toBoolean(String value) throws IllegalArgumentException {
        if (value == null || value.equals("")) {
            //the option not specified or a blank value specified
            return false;
        }

        if (value.equalsIgnoreCase("true")) {
            return true;
        }

        if (value.equalsIgnoreCase("false")) {
            return false;
        }

        throw new IllegalArgumentException("invalid boolean value : " + value);
    }
}

Related

  1. toBoolean(String string, boolean defaultValue)
  2. toBoolean(String text)
  3. toBoolean(String val)
  4. toBoolean(String value)
  5. toBoolean(String value)
  6. toBoolean(String value)
  7. toBoolean(String value)
  8. toBoolean(String value)
  9. toBoolean(String value)