Question

I have wrote code to get Latitudeand Longitude coordinates from a text file using the following code;

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);
CoordinatesParams tmp = new CoordinatesParams();

while(sc.hasNextLine())
{
    String pair = sc.nextLine();
    String[] s = pair.split("\t");
    tmp.setLatitude(s[0]);
    tmp.setLongitude(s[1]);     
}

I have also created a local List variable List<String> = coord and aafter the while loop i tried;

coord = new ArrayList<String>(Arrays.asList(new String[]{tmp.getLatitude(), tmp.getLongitude()}));

If i run a System.out.println(coord);all I get is [51.5109649, -0.14504045] while I have at least 50+ coordinates in my text file I was expecting the full list.

How come I am only receiving one value back? And how can I edit this to get all my values?

Was it helpful?

Solution

Do it like i have said

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 ); 
}

and then you can access them like:

    for(CoordinatesParamas c : lCoordinates)
        System.out.println(c.getLatitude()+" "+c.getLongtitude());

Actually your problem is that you got a variable temp and in the loop you write all the coordinates to this object, but never store the object with the acutal values. instead you override the object with every iteration.

But if you really want to store all the coordinates in a list of strings. there is no need for the object which holds lat and long then you can easily do:

    List<String>coord = new ArrayList<String>();

    while(sc.hasNextLine())
    {
        String pair = sc.nextLine();
        String[] s = pair.split("\t");
        coord.add(s[0]);
        coord.add(s[1]);
    }

    System.out.println(coord);

and get the same result like printing them in the loop, but now you can use this list coord as often as you want because all coords are added. But pls try to use the object you have created or use a Map<String,String> which holds a mapping of lat and long.

*note: if you do coord = new ArrayList<String>(Arrays.asList(new String[]{tmp.getLatitude(), tmp.getLongitude()})); you are creating a new empty arraylist with one pair of lat and long every iteration of your whileloop! if you print them there, it works. but if you print them after the loop, the coord list will only hold the last lat and long.

OTHER TIPS

I tried putting the coord = new ArrayList<String>(Arrays.asList(new String[]{tmp.getLatitude(), tmp.getLongitude()})); into the while loop and printed it from there and it worked

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