a question about precision loss

hi

ive written this simple method dealing with rectangles, but im getting a loss of precision error... i understand that the get methods return doubles but ive tried casting it as an integer, ie (Integer) at the start of the value statement but it doesnt sort it... im a little stumped, can anyone offer any advice?

heres the code:

public int randYAlgae(int index)

{

int pick = rand.nextInt(2);

if(pick == 0)

return (((Rectangle)algaeset.get(index)).getY()) + 5;

else if (pick == 1)

return (((Rectangle)algaeset.get(index)).getY()) - 5;

else return (((Rectangle)algaeset.get(index)).getY());

}

[666 byte] By [jschwerdt] at [2007-9-27 17:22:56]
# 1
oh, and ive tried changing the return type, it just causes more problems along the line...
jschwerdt at 2007-7-6 12:18:36 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
Acouple things. you could try: return (int) (((Rectangle)algaeset.get(index)).getY()) + 5;or: return (((Rectangle)algaeset.get(index)).y) + 5;hth,m
wywiwyg at 2007-7-6 12:18:36 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
crapthats embarrassing i totally forgot the cast syntaxcheers
jschwerdt at 2007-7-6 12:18:36 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

Try this:

return (int)((((Rectangle)algaeset.get(index)).getY()) + 5);

You have to make sure the entire expression is cast to an int before you return it. Another option is

return ((Rectangle)algaeset.get(index)).y + 5;

getY() returns a double, but it you access the y member variable directly, you get an int.

Jetset_Willy at 2007-7-6 12:18:36 > top of Java-index,Archived Forums,New To Java Technology Archive...