views:

1491

answers:

11

I did a little search on this but couldn't find anything useful.

The point being that if String value is either "true" or "false" the return value should be true. In every other value it should be false.

I tried these:

String value = "false";
System.out.println("test1: " + Boolean.parseBoolean(value));
System.out.println("test2: " + Boolean.valueOf(value));
System.out.println("test3: " + Boolean.getBoolean(value));

All functions returned false :(

+1  A: 
if ("true".equals(value) || "false".equals(value)) {
  // do something
} else {
  // do something else
}
+10  A: 
return "true".equals(value) || "false".equals(value);
Bombe
Instead of hard-coding `"true"` and `"false"`, you can use `true.toString` and `false.toString`, just in case the string representations of "true" and "false" ever change ;)
Ben James
In other languages, "1" and "0" are also considered valid for boolean string parsing, so I don't quite like the "hardcode true and false"-approach. OTOH the OP introduced the restriction to true and false, so well...
Thorsten Dittmar
I was hoping to avoid this, since the input value could also be " true" or "True" or "TRUE" etc.
Ragnar
@Ragnar As I pointed out in a comment above, you should use String.equalsIgnoreCase(String s).
moxn
@moxn Thanks. I'll probably just wrap this into one funtion to check both true and false so I get bit cleaner code.
Ragnar
A: 
return value.equals("false") || value.equals("true");
David Hedlund
Beware of `value == null`
You could wrap the return statement into an try/catch construct, catch NPE and handle this exceptional state to return the real ... just kidding ;-)
Andreas_D
@lutz - bad advice. A `null` is almost certainly a bug and should result in an NPE. Testing for `null` to avoid the NPE is almost certainly going to obscure the bug. You should only test for `null` if it is explicitly stated that `null` has meaning.
Stephen C
+2  A: 

Yes, but, didn't you parse "false"? If you parse "true", then they return true.

Maybe there's a misunderstanding: the methods don't test, if the String content represents a boolean value, they evaluate the String content to boolean.

Andreas_D
I kind of figured it out too.
Ragnar
+4  A: 

The methods you're calling on the Boolean class don't check whether the string contains a valid boolean value, but they return the boolean value that represents the contents of the string: put "true" in string, they return true, put "false" in string, they return false.

You can surely use these methods, however, to check for valid boolean values, as I'd expect them to throw an exception if the string contains "hello" or something not boolean.

Wrap that in a Method ContainsBoolString and you're go.

EDIT
By the way, in C# there are methods like bool Int32.TryParse(string x, out int i) that perform the check whether the content can be parsed and then return the parsed result.

int i;
if (Int32.TryParse("Hello", out i))
  // Hello is an int and its value is in i
else
  // Hello is not an int

Benchmarks indicate they are way faster than the following:

int i;
try
{
   i = Int32.Parse("Hello");
   // Hello is an int and its value is in i
}
catch
{
   // Hello is not an int
}

Maybe there are similar methods in Java? It's been a while since I've used Java...

Thorsten Dittmar
A: 

Something you should also take into consideration is character casing...

Instead of:

return value.equals("false") || value.equals("true");

Do this:

return value.equalsIgnoreCase("false") || value.equalsIgnoreCase("true");
Stuart
+9  A: 
  • parseBoolean(String) returns true if the String is (case-insensitive) "true", otherwise false
  • valueOf(String) ditto, returns the canonical Boolean Objects
  • getBoolean(String) is a red herring; it fetches the System property of the given name and compares that to "true"

There exists no method to test whether a String encodes a Boolean; for all practical effects, any non-"true"-String is "false".

mfx
OK, this what I needed to know. I wonder why it is practical to have have any non-"true"-String as false? I mean in my mind valid inputs should be true and false and everything else is just not boolean input.
Ragnar
True, one could imagine a "BooleanFormatException" Exception, in analogy to the NumberFormatException that occurs when you try to parse a non-number String. I suppose it was deemed unnecessary at the time the library was written (1996!), for the targeted environments (embedded systems like set-top boxes and browsers).
mfx
A: 

I suggest that you take a look at the Java docs for these methods. It appears that you are using them incorrectly. These methods will not tell you if the string is a valid boolean value, but instead they return a boolean, set to true or false, based on the string that you pass in, "true" or "false".

http://www.j2ee.me/javase/6/docs/api/java/lang/Boolean.html

hoffmandirt
A: 

Actually, checking for a Boolean type in a String (which is a type) is impossible. Basically you're asking how to do a 'string compare'.

Like others stated. You need to define when you want to return "true" or "false" (under what conditions). Do you want it to be case(in)sensitive? What if the value is null?

I think Boolean.valueOf() is your friend, javadoc says:

Returns a Boolean with a value represented by the specified String. The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Example: Boolean.valueOf("True") returns true.
Example: Boolean.valueOf("yes") returns false.

Stefan Hendriks
A: 

Using the boolean type

public class MainClass { public static void main(String args[]) { boolean b;

b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);

// a boolean value can control the if statement
if(b) System.out.println("This is executed.");

b = false;
if(b) System.out.println("This is not executed.");

// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));

} }

http://java.pakcarid.com/Cpp.aspx?sub=180&ff=910&topid=32&sls=24

lois