• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Float.valueOf() method and NumberFormatException

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


We don't need to put the post-fix f in the 3rd line. Why is that, What's really happening?

Thanks in Advanced!
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below are the lines pasted from java doc

"Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is not equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string
"1.00000017881393421514957253748434595763683319091796875001d"
results in the float value 1.0000002f; if the string is converted directly to float, 1.0000001f results."
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ramesh maredu, So the compiler won't check whether the String literal that we passed to the method has post-fixed "f". It convert the String to that Wrapper class object, on which the method being invoked!

Please Confirm!

Thanks in Advanced!

BTW, Why don't I get Notification E-mail for the "Watching Posts"?
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you can help me help you (I'm also studying for SCJP 6)

Chapter 3 of Sierra/Bates says:

Float f2 = Float.valueOf("3.14f"); // assigns 3.14 to the Float object f2



A page or two later, the text says:

Float f2 = new Float(3.14f); // make a new wrapper object
short s = f2.shortValue(); // convert f2's value to a short
System.out.println(s); // result is 3 (truncated, not // rounded)



I tried the following code snippit:



And it outputs:

Float.valueOf("3.14f") = 3.14
Float.valueOf(3.14f) = 3.14
Float.valueOf("3.14") = 3.14



However, if I add the following code:




I get a compiler error:

cannot find symbol
symbol : method valueOf(double)
location: class java.lang.Float
Float f5 = Float.valueOf(3.14);
^
1 error



I'm really not sure why this is happening....

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandra, from what I understood, on the f5 variable you are passing a double parameter for the Float.valueOf() method, while it does not support it.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no this is not the case
this is because ValueOf method takes the String as the argument and not others
so
 
Rene Argento
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I said. The Float.valueOf() method accepts either a float or a String parameter though.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah
Rene
nice work
I missed it
Happy preparation
 
Rene Argento
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
But I still have a long way to go before getting ready for SCJP..
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

We don't need to put the post-fix f in the 3rd line. Why is that, What's really happening?

Thanks in Advanced!



I think, here, we are using name reference Float, so the method valueOf() takes a String arguments and convert it to the named class, So no need of postfix "f" at the end. it's redundant!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:
We don't need to put the post-fix f in the 3rd line. Why is that, What's really happening?



I assume you folks already got the point. Just clear up the things. There in the third line, whether you put a f or not it wouldn't change the type of argument you are passing. That would still be a string. The whole point of passing a f is to make the number float, otherwise a decimal literal is of double type. And there is no method like, Float.valueOf(double), exists.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adeel Ansari wrote:

Abimaran Kugathasan wrote:
We don't need to put the post-fix f in the 3rd line. Why is that, What's really happening?



I assume you folks already got the point. Just clear up the things. There in the third line, whether you put a f or not it wouldn't change the type of argument you are passing. That would still be a string. The whole point of passing a f is to make the number float, otherwise a decimal literal is of double type. And there is no method like, Float.valueOf(double), exists.



Exactly.

When I post this question, I've this doubt, but I cleared it.

Thanks to All......
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic