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 :) .