I am having a hard time getting this assignment done for school. There is more to it than just what is in the title, but basically this is what I need to do first. Read each line of the text file that have an x and a y point, extract them, and store them in a new node. I then need to add this new node into a linked list and repeat for every x y coordinate. No arrays are to be used.
This is the code I have so far:
public class Point {
public int data;
public Point next;
public Point(int data, Point next) {
this.data = data;
this.next = next;
}
@Override
public String toString() {
return data + "";
}
}
public class ShapeAbstraction {
public void readCoordinates() {
String fileName = "shapelist.txt";
String line = null;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file " + fileName);
}
catch(IOException ex) {
System.out.println("Error reading file" + fileName);
}
}
public static void main(String[] args) {
ShapeAbstraction sa = new ShapeAbstraction();
sa.readCoordinates();
Point front = new Point(0, null);
System.out.println(front);
}
}
The coordinates in the .txt file look like this
10 0
18 0
17 2
21 7
19 12
That's only the first 5 out of hundreds.
If someone could help that would be amazing. Thanks. Assignment for reference BTW we are now allowed to use the java linked list class.