Question

I am trying to make a list of coordinates from a text file and I want to be efficient with it, so I created a class that has a latitude and longitude double variables, along with their getter and setter methods.

So in the other class I make an object of the previous class to use the setter methods.

CoordinatesParams params = new CoordinatesParams();

How can I read the list of coordinates from the text file and set them to the latitudeand longitudevariables?

Sorry if this question is very basic to some

File Bus_Routes = new File("C:/Users/Daniel Dold/Desktop/Routes/Bus_Routes.txt");
Scanner scanner = new Scanner(Bus_Routes);

String line = scanner.nextLine();
String[] parsed = line.split("\\s");
String routeText = parsed[0];

String dir = "C:/Users/Daniel Dold/Desktop/Routes/";
File routeFile = new File(dir, routeText);
Scanner sc = new Scanner(routeFile);
while(sc.hasNextLine())
{
    String line2 = sc.nextLine();
    String[] s = line2.split("\t");
}

This is what I have so far to print the results on the file.

51.50177649 -0.05012445
51.50210374 -0.05050666
51.50253617 -0.0509908
51.50265346 -0.05072191
51.50274404 -0.05055025
51.50301702 -0.05011841

The coordinates file are just 2 columns the first being latitude and the second longitude

Was it helpful?

Solution

I dont know how the File looks inside, but you should creat a list for your CoordinatesParamas and then iterate over the data you read from the file to fill the list with CoordinatesParamas objects and set the latitude and longitude in this objects via setters.

List<CoordinatesParamas> lCoordinates = new ArrayList<CoordinatesParamas>();

while(sc.hasNextLine()) {
     CoordinatesParamas temp = new CoordinatesParamas();
     String pair = sc.nextLine();
     String[] s = pair.split(" ");
     temp.setLatitude(s[0])
     temp.setLongitude(s[1])
     lCoordinates.add(temp ); 
}

I changed string.split("\t") with split(" ") because it looks like there is not a tab between the coordinates, only a whitespace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top