BBC (Brian's Boot Camp)

Not to be confused with the British Broadcasting Corporation, the BBC will focus on Brian's fast paced journey of the mastery of the Java programming language.

Thursday, February 24, 2005

Parsing a string for boolean

In the examples in the 21 Days book, there are numerous instances where a String is parsed to produce some other value type like an int or a float using a statement such as i = int.parseInt(string). This makes perfect sense to me.

One of the exercises says this: Create a modified version of the Storefront project that includes a noDiscount variable for each item. When this variable is true, sell the item at the retail price.
Seems simple enough. I took the GiftShop class which defines the items for sale and added a "TRUE" or "FALSE" to each of them; that's not where the problem is. Through a couple of method calls the string values assigned to each item in GiftShop get handed off to an Item class. Item takes all of the strings from GiftShop and assigns them to String variables or parses them and assigns them to ints or doubles. Both of those two operations use the following two statements:
retail = Double.parseDouble(retailIn);
quantity = Integer.parseInt(quanIn);

Pretty straightforward at this point.

So I get to thinking, "Well I know that ints and doubles are primitive but that there are objects which correspond to each of the primitives; that's what the whole Double.parseDouble bit is about. I know there is also a Boolean object, so I'll do the same thing for my noDiscount variable: I'll use something like this:
private boolean = noDiscount;
/* unrelated code
when the String values come in from GiftShop, the "TRUE" or "FALSE" value comes in as "String discountIn"
so then I do this: */
noDiscount = Boolean.parseBoolean(discountIn);


But it doesn't work. When I compile the class, I get the following error back:

Item.java:18: cannot resolve symbol
symbol : method parseBoolean (java.lang.String)
location: class java.lang.Boolean
noDiscount = Boolean.parseBoolean(discountIn);
^
1 error


But when I look at the API documentation, the method is there and is listed exactly the same as is the equivalent method for Integer:
static boolean | parseBoolean(String s) | Parses the string argument as a boolean.
and
static int | parseInt(String s) | Parses the string argument as a signed decimal integer.

What gives?

5 Comments:

Blogger Brian T. Grant said...

And don't go telling me that Boolean.parseBoolean(); is new with Java 1.5 'cuz that's just going to piss me off.

5:51 AM  
Blogger Brian T. Grant said...

Well shit.

5:56 AM  
Blogger Sten said...

I thought that was going to be a link to some site about defecation. Oh well.

Once again I'm thoroughly impressed by not only your questions and frustrations, but by the steps you took to solve the problem. Your reasoning was exactly correct every step of the way.

Specifically I like that you inferred that there should have been a parseBoolean(), and that you went to the API documentation to verify its usage.

So yeah, pre-1.5 you could either do the new Boolean(val).booleanValue(), or if you don't want to create a boolean object (which isn't typically an issue), you could write your own that would do the same thing:

public boolean parseBoolean(String val) {

return val != null && val.equalsIgnoreCase("true");

}

And to speak to that guy's comment on the web site, the System class is for functions related to the underlying system (like accessing the system clock or loading native libraries). Parsing primitives appropriately belong in their corresponding wrapper classes.

7:39 AM  
Blogger Brian T. Grant said...

Here is the author's solution to this problem. I generally try to stay away from looking at his answers if I can -- I'd rather figure it out on my own. In lieu of having Boolean.parseBoolean(); available, this makes pretty good sense and it is simple:
if (discountIn.equals("TRUE"))
noDiscount = true;
else
noDiscount = false;

9:00 AM  
Blogger Brian T. Grant said...

Thank you very much for the compliment, by the way. Your positive reinforcement is valuable to me for numerous reasons ;)

9:01 AM  

Post a Comment

<< Home