aspose file tools*
The moose likes Beginning Java and the fly likes valueOf() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "valueOf()" Watch "valueOf()" New topic
Author

valueOf()

AJKC Shekhawat
Greenhorn

Joined: Jan 10, 2006
Posts: 7
String txt = "Honey ";

System.out.println("double for the above string is: " + Double.valueOf(txt));

why doesn't it execute.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

"Honey" doesn't look like a parsable number to to me


[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Do you know what the method Double.valueOf() does with the string that to supply to it? If not, read the API documentation of that method, and you'll understand why giving it a string like "Honey " doesn't work.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Or, with other words: What would you expect it to do?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

i would have no idea what the decimal value of the word "honey" is either, if somebody asked me.

the method you are calling tries to read the string and make it into a number. its looking for digits, a possible decimal point, possibly a +/- sign.

how is it supposed to convert letters to numbers?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Fred]: how is it supposed to convert letters to numbers?

Well, if we get rid of the space at the end of "Honey ", and use an Integer or Long rather than a Double, then we could parse it as a number in base 35 or higher...


"I'm not back." - Bill Harding, Twister
Joseph Sillitoe
Greenhorn

Joined: Dec 30, 2005
Posts: 12
Well, if we get rid of the space at the end of "Honey ", and use an Integer or Long rather than a Double, then we could parse it as a number in base 35 or higher...


pffft. Only if we assume that it is not case sensitive. Otherwise we obviously cannot parse this in any number system less than base 61.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
[Fred]: how is it supposed to convert letters to numbers?

Well, if we get rid of the space at the end of "Honey ", and use an Integer or Long rather than a Double, then we could parse it as a number in base 35 or higher...


Have you tried

Integer.parseInt("Honey", 35);

?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sure did. It's 26568324. Using base 36, it's 29703562. Beyond that, we exceed Character.MAX_RADIX, it turns out. So saying "base 35 or higher" was a bit misleading, as only 35 or 36 are possible.

Do you get a different result, Ilja?

[Joseph]: Only if we assume that it is not case sensitive.

Hey, I'm still flexible on assumptions here - I haven't even committed to a particular base yet. But if we're talking about the parseXxx() methods (and I did say I was using Integer, or Long), it's documented that these are case insensitive. See details under Character.digit().

My apologies to the original poster, as this little discussion is almost certainly of no interest to you. The point is - what would you expect Double.valueOf() to do here?
[ January 10, 2006: Message edited by: Jim Yingst ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by AJKC Shekhawat:
String txt = "Honey ";

System.out.println("double for the above string is: " + Double.valueOf(txt));

why doesn't it execute.


Actually, this DOES execute. However, I think you are confused about the result, since it is probably not what you expected. So what DO you expect this to do? Please helpe us help you by providing some more details.

Thanks,

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: valueOf()