This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing from Double to Int  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have an array of double values and i am then using this array within a switch statement...
switch(timevaluesarray(iCount) {
...........
}
When I compile the code I get the error 'loss of precision', where a double is found but and int is required. Is there a simple line of code that can change the values in the array from double to int??
The values being fed into the array are required to be double so i can't change them before they are put into the array.
Thanks for any help!
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am guessing that the method timevaluesarray(iCount) returns a double value, if that is the case you can always try casting like this:
switch((int)timevaluesarray(iCount) {
...........
}
[ March 13, 2004: Message edited by: Vicken Karaoghlanian ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, be very clear about what the compiler warning concerning the possible loss of precision means. Values that were different will match the same case. In other words, casting a double to an int and then switching on the resulting value might not work as hoped since values like 2.4 and 2.3 will both match case 2 in the switch construct.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!