Some of the file I'm working with: http://pastebin.com/WriQcuPs
Currently I had to make the population, latitude, and longitude strings or else I wouldn't get the desired output. I want for them to be int, double, and double in that order.
public class City {
String countrycode;
String city;
String region;
String population;
String latitude;
String longitude;
public City (String countrycode, String city, String region, String population, String latitude, String longitude) {
this.countrycode = countrycode;
this.city = city;
this.region = region;
this.population = population;
this.latitude = latitude;
this.longitude = longitude;
}
public String toString() {
return this.city + "," + this.population
+ "," + this.latitude + ","
+ this.longitude;
}
}
I suspect it has something to do with how I created the array list. Is there a way to make it so that some of the elements of the list are of a different type? I tried changing it to ArrayList and changing the data types in the City class but it still gave me a ton of errors.
public class Reader {
In input = new In("file:world_cities.txt");
private static City cityInfo;
public static void main(String[] args) {
// open file
In input = new In("world_cities.txt");
input = new In("world_cities.txt");
try {
// write output to file
FileWriter fw = new FileWriter("cities_out.txt");
PrintWriter pw = new PrintWriter(fw);
int line = 0;
// iterate through all lines in the file
while (line < 47913) {
// read line
String cityLine = input.readLine();
// create array list
ArrayList cityList = new ArrayList(Arrays.asList(cityLine.split(",")));
// add line to array list
cityList.add(cityLine);
// increase counter
line += 1;
// create instance
cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), cityList.get(3), cityList.get(4), cityList.get(5));
System.out.println(cityInfo);
// print output to file
pw.println(cityInfo);
}
// close file
pw.close();
}
// what is printed when there is an error when saving to file
catch (Exception e) {
System.out.println("ERROR!");
}
// close the file
input.close();
}
}
解决方案
If you declare the list as follows, you can put instances of any reference type into it:
List list = new ArrayList<>();
But the downside is that when you get an element from the list, the static type of the element will be Object, and you will need to type cast it to the type that you need.
Also note, that you can't put an int or a double into a List. Primitive types are not reference types, and the List API requires the elements to be instances of reference types. You need to use the corresponding wrapper types; i.e. Integer and Double.
Looking at more of your code, I spotted this:
ArrayList cityList =
new ArrayList(Arrays.asList(cityLine.split(",")));
If you change the list to List where the objects are either Integer or Double, you won't be able to build your list like that.
In fact, the more I look this, the more I think that you don't need a list at all. You should be doing something like this:
// read line
String cityLine = input.readLine();
String[] parts = cityLine.split(",");
// increase counter
line += 1;
// create instance
cityInfo = new City(parts[0], parts[1], parts[2],
Integer.parseInt(parts[3]),
Double.parseDouble(parts[4]),
Double.parseDouble(parts[5]));
Notice: there is no List there at all!!