• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

throws NumberFormatException ???

 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Float f = new Float("-2.0f");
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not getting any exception with
Float f = new Float("-2.0f");
nor with
Float f = new Float(-2.0f);
it prints -2.0
-PC
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i wanted to know why doesn't it throw the NumberFormatException? The API mentions that the NumberFormatException is thrown if the string does not contain a parsable number.
for e.g.
new Float("0x0123") throws the NumberFormatException
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Float

Constructs a newly allocated Float object that represents the floating-point value of type float represented by the string. The string is converted to a float value as if by the valueOf method.
Parameters:
s - a string to be converted to a Float.
Throws:
NumberFormatException - if the string does not contain a parsable number.
See Also:
valueOf(java.lang.String)


I believe -2.0f is valid as a float literal, so it's parsable.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API documentation for Float.valueOf defines valid String values for the constructor Float(String), parseFloat(String) and valueOf(String).
The wrapper classes are inconsistent. The long suffix L is not allowed, but the float F and double D suffixes are allowed.
[ August 29, 2003: Message edited by: Marlene Miller ]
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The wrapper classes are inconsistent. The long suffix L is not allowed, but the float F and double D suffixes are allowed.



Thanks Marlene. I got it now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic