Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

valueOf()

 
AJKC Shekhawat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String txt = "Honey ";

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

why doesn't it execute.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Honey" doesn't look like a parsable number to to me
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, with other words: What would you expect it to do?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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...
 
Joseph Sillitoe
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic