Alex Rivera | Logout

How to check if String value is Boolean type in Java?

Asked 2009-11-19T12:13:28.127
55

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 :(

Edit
Report

2 Answers

2

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.

answered 2009-11-19T12:16:53.550
1

Well for this, also have a look at org.apache.commons.lang.BooleanUtils#toBoolean(java.lang.String), along with many other useful functions.

answered 2013-01-09T10:01:53.570

Your Answer