Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting problem

 
Amar Deep Singh Chaudhary
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers !
i have a problem in casting the double data type into the string type.
i have write a code as
double i=100*Math.random();
String a=i.toString();
but it is throwing an error .please give anybody give the suggestion that how i can convert a random number into a string type.
thanks in Advance
Amar Deep Chaudhary
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't call a method on a primitive.

If you look at the String API, you will find an overloaded method called valueOf that will let you convert any type to a String.
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moreover if you want a more strict control over type you can use also
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic