• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One more question, then thats it I promise :) ...

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay this program is supposed to take any integer entered by a user, then strip off the individual digits and sum them up. I just can't work out this last error I keep getting. Any idea how to fix it? Heres the code and the error message...


C:\My Documents\Homework 6\IntSum2.java:32: possible loss of precision
found : double
required: int
num = (InputNum / Math.pow(10, count) % 10);
^
1 error
Not sure why its telling me loss of precision, I think the equation is okay the way it is. I didnt start getting the error untill I put Math.pow into the equation.
 
Amit KumarS
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hiii..
If you go through JavaDocs then you will find that Math.Pow() return a double and in this case you need to are storing the value in an int datatype.
tgus according to Java when u r storing a bigger datatype into a smaller in which there is possibility of loss of precision then you need to explicitly type cast it..
so u can do this in any of the following manner and your error will be gone..

i hope i have answered your question... if still in doubt please get back
Thanks
Amit
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wonderful, it compiles! thank you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic