Reputation: 727
I am trying to create several points in a java program. The coordinates of the points are in a text file, which I scan in and read number by number.
double x = 0.0;
double y = 0.0;
Point origin = new Point(x, y);
Point[] points = new Point[1000]; // There are 1000 points in total, thus 2000 doubles, this array will be used to store all the points
Scanner keyboard = new Scanner(System.in);
String FileName = keyboard.nextLine();
Scanner linReader = new Scanner(new File(FileName));
while (linReader.hasNextDouble()) {
x = linReader.nextDouble();
y = linReader.nextDouble();
origin = (x, y); // error telling me 'cannot convert from double to point'
}
I get the error "cannot convert from double to a point" so I need to know how to I fix this error? Am I allowed to use doubles coordinates for points?
Upvotes: 2
Views: 11640
Reputation: 11
Use this class:
import org.springframework.data.geo.Point;
maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Upvotes: 0
Reputation: 460
Heres a minimum change solution:
double x = 0.0;
double y = 0.0;
Point origin;
Point[] points = new Point[1000]; // There are 1000 points in total, thus 2000 doubles, this array will be used to store all the points
Scanner keyboard = new Scanner(System.in);
String FileName = keyboard.nextLine();
Scanner linReader = new Scanner(new File(FileName));
while (linReader.hasNextDouble()) {
x = linReader.nextDouble();
y = linReader.nextDouble();
origin = new Point(x, y);
}
You could also implement your own Point class something like this
public class Point {
double x;
double y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
}
Hope this helps
Upvotes: -1
Reputation: 1143
Point
class won't give the double precision. For the double precision you need to use Point2D.Double class. Consider the following code as example.
static void pointTest() {
double x = 1.2;
double y = 3.4;
Point2D.Double pointDouble = new Point2D.Double(x, y);
System.out.println(pointDouble);
}
Upvotes: 3
Reputation: 18792
public static void main(String args[]) {
//// Alternative 1
double x = 9.7;
double y = 8.6;
//import com.sun.javafx.geom.Point2D;
Point2D point2D = new Point2D();
point2D.x = (float) x; point2D.y= (float) y;
///// Alternative 2 (lose some accuracy)
//use Double object
Double x1 = 9.7;
Double y1 = 8.6;
//import java.awt.Point;
Point origin = new Point(x1.intValue(), y1.intValue());
}
Upvotes: 0
Reputation: 1780
Seems more straightforward to do it like this:
List<Point> points = new ArrayList<Point>(1000);
...
points.add(new Point(x, y));
Upvotes: -1