Simon Hattori Wilson Simon Hattori Wilson - 1 month ago 15
Java Question

incompatible types: possible lossy conversion from double to int. I don't know why i'm getting this error message

private void buildingComposotion ()

{
for (int i=1; i<=randomInteger(3,6); i++)
{
int numberOfStories = 2;
buildingFrame(numberOfStories);
buildingWindows(numberOfStories);
}
}
private int randomInteger(int min, int max)
{
min = (int) Math.ceil(min);
max = (int) Math.floor(max);
return Math.floor(Math.random() * (max - min));
}


The error, "incompatible types: possible lossy conversion from double to int." is on line 15.

Answer

Math.floor returns a double, not an int. Modify your randomInteger method:

private int randomInteger(int min, int max) 
{
    min = (int) Math.ceil(min);
    max = (int) Math.floor(max);
    return (int) Math.floor(Math.random() * (max - min));
}

Alternatively, use nextInt from the Random class