Steve McHam Steve McHam - 4 months ago 17
Java Question

How to get double precision number from Point object

I have a Point object and I want it to return a double to my variable

x
. How do I do it without casting it?

import java.awt.Point;

public class Main {
public Main() {
Point point = new Point(1, 1);
Double doub = point.x; // Dosent work. Returns an int

}

public static void main(String[] args){
Main main = new Main();
}
}


I get the error: "Cannot convert int from double"

Answer

Use the getter function in the point object. This will return a double

import java.awt.Point;

public class Main {
    public Main()  {
      Point point = new Point(1, 1);
      Double doub = point.getX(); // Returns double, You dont have to cast

    }

    public static void main(String[] args){
        Main main = new Main();
    }
}
Comments