0

I have a program that reads a number of cities from a file. However, it only reads the last element and prints it as many times as the lines of the file. My guess is that there is a problem with the reference of String objs but I have no idea how to solve this (if it ends up being the problem). Any suggestions would be much appreciated. Here is the code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.io.*;

public class Start {

    public static void main(String[] args) throws IOException {
        ArrayList<City> list=new ArrayList<City>();
        File file = new File("test.txt");
        if(file.exists()) {
            try{
                load(file, list);
            }catch(IOException e) {
                System.out.println("There was an error loading the file");
                System.exit(0);
            }
            System.out.println("Data have been found in the machine and have been loaded succesfully");
        }
        else {
            System.out.println("No data have been found on the machine");
        }
        City obj;
        ListIterator<City> i = list.listIterator();
        while(i.hasNext()) {
            obj = i.next();
            System.out.println(obj.getName()+" "+obj.getCountry());
        }


    }

    static void load(File fl, List<City> ct) throws IOException {
        BufferedReader bf = new BufferedReader(new FileReader(fl));
        String str;
        String[] objs = new String[8];
        City city = new City("nothing", "nothing", 0, 0, "nothing", 0, 0, 0);
        while((str=bf.readLine()) != null) {
            objs = str.split(" ");
            city.setName(objs[0]);
            city.setCountry(objs[1]);
            city.setCafeRestaurants(Integer.valueOf(objs[2]));
            city.setLatitude(Double.valueOf(objs[3]));
            city.setLongitude(Double.valueOf(objs[4]));
            city.setMuseums(Integer.valueOf(objs[5]));
            city.setParks(Integer.valueOf(objs[6]));
            city.setWeather(objs[7]);
            ct.add(city);
        }
        bf.close();
    }

The City class:


public class City {
    String name;
    String country;
    int museums;
    int cafeRestaurants;
    String weather;
    int parks;
    double latitude;
    double longitude;

    public City(String name,String country,int museums, int cafeRestaurants, String weather, int parks, double latitude, double longitude) {
        this.name = name;
        this.country = country;
        this.museums = museums;
        this.cafeRestaurants = cafeRestaurants;
        this.weather = weather;
        this.parks = parks;
        this.latitude = latitude;
        this.longitude = longitude;
    }


    public City(int museums, int cafeRestaurants, String weather, int parks) {
        super();
        this.museums = museums;
        this.cafeRestaurants = cafeRestaurants;
        this.weather = weather;
        this.parks = parks;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getCountry() {
        return country;
    }


    public void setCountry(String country) {
        this.country = country;
    }
    public int getMuseums() {
        return museums;
    }

    public void setMuseums(int museums) {
        this.museums = museums;
    }

    public int getCafeRestaurants() {
        return cafeRestaurants;
    }

    public void setCafeRestaurants(int cafeRestaurants) {
        this.cafeRestaurants = cafeRestaurants;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public int getParks() {
        return parks;
    }

    public void setParks(int parks) {
        this.parks = parks;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    } 

    public void setDefault() {
        this.setCafeRestaurants(0);
        this.setMuseums(0);
        this.setParks(0);
        this.setWeather("Sunny");
    }

}

And the test.txt:

Athens Greece 0 123.123 123.123 0 0 Sunny
Tokyo Japan 0 123.123 123.123 0 0 Sunny

I understand that the data are not correct but those are dummy data so I hope you can forgive me for that :) .

Azzarian
  • 153
  • 2
  • 13
  • 1
    Does this answer your question? [Why does my ArrayList contain N copies of the last item added to the list?](https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – Savior Apr 13 '20 at 14:33
  • 2
    You only create ***one*** City object, and so only one is added multiple times to your ArrayList. You need to create a new City object ***within*** the loop so that each separate city's data gets its own City object added to the list. – Hovercraft Full Of Eels Apr 13 '20 at 14:35

0 Answers0