> Separate the trim() from the valueOf(), and use the default value when
> trim() results in an empty string:
[quoted text clipped - 16 lines]
>
> /gordon
Thank you. I was surprised that, why Double.valueOf("") is not 0.0?
instead it throws an Exception!
Could you help me a little more? Thank you.
Gordon Beaton - 02 May 2007 14:37 GMT
> Thank you. I was surprised that, why Double.valueOf("") is not 0.0?
> instead it throws an Exception!
At the risk of stating the obvious, the string "" does not represent
any number at all. You've chosen to interpret it as 0.0, but that's up
to you and not necessarily a universal interpretation. In many cases a
missing value is an error.
Double.valueOf() is defined to return a value according to the syntax
rules of the JLS. In the Java language you can't just leave out a
value when you want to specify 0, and the same applies to
Double.valueOf().
/gordon
--
Patricia Shanahan - 02 May 2007 15:30 GMT
>> Separate the trim() from the valueOf(), and use the default value when
>> trim() results in an empty string:
[quoted text clipped - 20 lines]
> instead it throws an Exception!
> Could you help me a little more? Thank you.
The simple answer is just that those are the rules - see the
Double.valueOf documentation,
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#valueOf(java.lang.
String)
However, you probably want to know why those are the rules.
I don't think there is any one-size-fits-all right answer to the
handling of an empty string, or an all space string. In that sort of
situation, it is usually better to err on the side of reporting the
problem, to bring it to the programmer's attention. The programmer can
then decide the right handling on a case-by-case basis.
Incidentally, Double.valueOf ignores leading and trailing whitespace, so
normally there is no need to trim. However, in this case it simplifies
the all space test.
Patricia
Tor Iver Wilhelmsen - 02 May 2007 20:54 GMT
På Wed, 02 May 2007 15:25:13 +0200, skrev www <www@nospam.com>:
> Thank you. I was surprised that, why Double.valueOf("") is not 0.0?
> instead it throws an Exception!
> Could you help me a little more? Thank you.
What makes 0.0 a more likely answer to "what is the number represented by
nothing" than, say, not-a-number (Double.NaN)? Since someone would prefer
one and others the other, the library develoers chose to throw an
exception instead, in order to say "don't do silly stuff like that".