This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes parseXxx() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parseXxx()" Watch "parseXxx()" New topic
Author

parseXxx()

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi all,



Why does this work


while this does not


Thanks.


Be Humble... Be Nice.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In this code, dd (without quotes) is a variable that holds a value of 3.2. But "dd" (with quotes) is a String literal that has nothing to do with the variable or its value. So "dd" has no meaning as a float.

"3.2" (with quotes) is also a String literal, but it is in a format that can be parsed to a float.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Thanks for replyin Marc. I get it now.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

If you want to assign the value of variable dd to your float, just assign it (with a cast to make sure it fits):


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: parseXxx()
 
Similar Threads
Float.valueOf is rounding 999999.99 to 1000000.00
Parsing String Representation of Number Literal with Suffices
quiz on Float.parseFloat(String)
string to float conversion round off problem
repeted values in jsp