• Post Reply Bookmark Topic Watch Topic
  • New Topic

String to boolean  RSS feed

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text field (More) in a form. The value of this text field is either 1 or 0. After users submit the form, my servlet reads the value of this text field as a String.
I want to convert the String to boolean and I have checked the API, it says that

valueOf(String s) returns a Boolean with a value represented by the specified String.
booleanValue() returns the value of this Boolean object as a boolean primitive.

Therefore, I think that I should:

Am I right?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your logic is correct, but your syntax is wrong.
boolean more =
Boolean.valueOf ( request.getParameter( "More" ) ).booleanValue();
But since getParameter() may return null, I think you ought to call that first, check the return value, then decide whether to call valueOf() or not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!