-1

I have the following code:

public void keyboard() {
    int sealX;
    double sealY;
    if (Greenfoot.isKeyDown("up")) {
        sealX = getX();
        sealY = getY();
        setLocation(sealX, (sealY - 1.25));
    }
    if (Greenfoot.isKeyDown("down")) {
        sealX = getX();
        sealY = getY();
        setLocation(sealX, (sealY + 1.25));
    }
}

Initially, the variable sealY was an integer, like sealX. However, 1 ended up being too small as an incremented in the two conditionals below, and 2 was too large.

The program compiled and worked fine before. But as soon as I changed sealY to a double and changed the operation that are done on keyboard input to numbers with decimals (doubles), Java started throwing the error - Incompatible types: possible lossy conversion from double to int

I've seen this before at times where I was using a double unnecessarily and an integer would have sufficed. However, that is not the case here. An integer will not suffice. I find that this isn't a fatal error and is more of a tip, but my program won't compile because of that.

Personally, I think the compiler can try to be helpful, but otherwise it's none of its business whether I use a double or an int. There should be a way to override the compiler if I insist upon using a double, particularly because I can't use an integer for what I am trying to do here.

I don't think casting will fix this problem. Is there a way to manually override compilers and declare that I know what I'm doing and I want sealY to be a double?

EDIT: enter image description here

And some documentation:

setLocation public void setLocation(int x, int y) Assign a new location for this actor. This moves the actor to the specified location. The location is specified as the coordinates of a cell in the world. If this method is overridden it is important to call this method as "super.setLocation(x,y)" from the overriding method, to avoid infinite recursion. Parameters: x - Location index on the x-axis y - Location index on the y-axis See Also: move(int)

InterLinked
  • 1,247
  • 2
  • 18
  • 50
  • Please post `getX()`. *I think the compiler can try to be helpful, but otherwise it's none of its business whether I use a double or an int.* Compiling the code **is** the compilers only business. – Elliott Frisch May 27 '17 at 23:18
  • Which line gives the error? Or, even better, can you reproduce what you're trying to do in a minimal self contained program – Joni May 27 '17 at 23:20
  • @Joni There's a lot of code involves and multiple classes so unfortunately I don't think so. The errors are both on the lines with the setLocation statement – InterLinked May 27 '17 at 23:23
  • 1
    Sounds like setLocation accepts int parameters, not double. Java will not convert double into int automatically because of lots of precision. It sounds like you want change the setLocation method so it accepts doubles instead of ints – Joni May 27 '17 at 23:31
  • Why don't you think casting will fix the problem? – Radiodef May 27 '17 at 23:46

1 Answers1

0

Casting won't solve your problem as well You have to use :

sealY = getY().doubleValue();

if getY() returns an int you should use :

sealY = new Integer(getY()).doubleValue();

EDIT :

it seems that your setLocation function looks like setLocation(int x, int y);

so to avoid this error you can only do :

setLocation(sealX, new Double(y + 1.25).intValue());

or as Radiodef said

setLocation(sealX, (int) (y + 1.25));

It looks like a dirty trick, but I don't know about any other solution

D. Peter
  • 487
  • 3
  • 12
  • That gives me the error "int cannot be dereferened". Now I actually have 3 errors instead of 2, although it seemed to work fine at first – InterLinked May 27 '17 at 23:16
  • getX() returns an int or Integer ? – D. Peter May 27 '17 at 23:17
  • getX() returns an integer, since that is how I use it and I have no problems, I would assume. To be clear, getX() needs to be an int while getY() needs to be a double – InterLinked May 27 '17 at 23:20
  • Sorry, I'm writing a solution please wait ;) – D. Peter May 27 '17 at 23:23
  • setLocation takes 2 doubles as parameters ? ok int and double ;) – D. Peter May 27 '17 at 23:24
  • No, setLocation simply uses the two variables - one int and one double, is that a problem? I implemented your 2nd line of code and still have the original 2 errors – InterLinked May 27 '17 at 23:25
  • where are the errors in your code ? I'm not able to reproduce them – D. Peter May 27 '17 at 23:32
  • There's an implicit conversion from `int` to `double` so the return value of `getY()` is not what's causing the compilation error. This is really just a weird way to do conversions too. What do you think this code does that casting wouldn't do, even if casting was necessary there? – Radiodef May 27 '17 at 23:41
  • Yes, thank you for your edit, it's more explict and easy to anderstand, I wrote an edit too – D. Peter May 27 '17 at 23:47
  • [This is just the same as a cast](http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#intValue--) but you are creating an object. If you look at [the source code for `Double`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Double.java#Double.intValue%28%29), you can even see that it just returns `(int) value`. – Radiodef May 27 '17 at 23:49