Q:

Creating linked list parsing line trouble

I am reading lines from a file, the total number is around 150,000 lines. Each time a line is read    
-i modify the object city, 
-then add it to the linked list
-then print out the content of the linked list in the last position, and I find it correct.
-after the filereader loop ends, i find that all the object on the linkedlist have the same city value, which is the last value entered ( last line of the file read) --> this is the problem
public static void lala() throws IOException{

    String FileLine;

    BufferedReader R = new BufferedReader(new FileReader(
            "list_of_1000_cities with coords.txt"));

    city x = new city() ;

    while ((FileLine = R.readLine()) != null) {
        String[] Tokens = FileLine.split(",");

        if (Preprocessor.get_double(Tokens[1])!=0 && Preprocessor.get_double(Tokens[2])!=0) {

        x.latitude= Double.parseDouble(Tokens[1]);

        x.longitude= Double.parseDouble(Tokens[2]);

        x.name= Tokens[0];
        citylist.add(x );

        System.out.println(citylist.get(citylist.size() - 1).name);
         //prints the correct name

        }
    }

        System.out.println(citylist.get(1115).name);

    // always prints the last name on the file read no matter how much i change the index printed 

}

A:

Java refers to objects by references. Hence within your while loop you are modifying the value of the same reference to X that you have assigned outside the loop.
Instantiate a new value of X within the while loop for each of the co-ordinates and then add it to the list - your problem will be solved.
To be more specific with your code :
String FileLine;
BufferedReader R = new BufferedReader(new FileReader(
        "list_of_1000_cities with coords.txt"));

while ((FileLine = R.readLine()) != null) {
    String[] Tokens = FileLine.split(",");

    if (Preprocessor.get_double(Tokens[1])!=0 && Preprocessor.get_double(Tokens[2])!=0) {

    city x = new city() ;

    x.latitude= Double.parseDouble(Tokens[1]);

    x.longitude= Double.parseDouble(Tokens[2]);

    x.name= Tokens[0];
    citylist.add(x );

    System.out.println(citylist.get(citylist.size() - 1).name);
     //prints the correct name

    }
}

    System.out.println(citylist.get(1115).name);

