I want to read a file in Java and put the numbers from the file into the (x,y) coordinates of a Point2D[] array. How would I go about doing this?
my file looks like:
5 4
6 2
4 1
which are points (5,4) (6,2) (4,1).
I want to read a file in Java and put the numbers from the file into the (x,y) coordinates of a Point2D[] array. How would I go about doing this?
my file looks like:
5 4
6 2
4 1
which are points (5,4) (6,2) (4,1).
Say you have a
Point
class that looks like something like this (you could also use thejava.awt.geom.Point2D
class but it is not advisable apparently) :And a file called
points.txt
that you want to parse with the following contents:You could use a
BufferedReader
to read the file line by line:Output: