我正在使用的一些文件:http://pastebin.com/WriQcuPs
目前我必须制作人口,纬度和经度字符串,否则我将无法获得所需的输出。我希望他们按顺序为int,double和double。
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;
}
}
我怀疑它与我创建数组列表的方式有关。是否有办法使列表中的某些元素具有不同的类型?我尝试将其更改为ArrayList<City>
并更改City
类中的数据类型,但它仍然给我带来了大量错误。
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<String> cityList = new ArrayList<String>(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();
}
}
答案 0 :(得分:1)
如果您按如下方式声明列表,则可以将任何引用类型的实例放入其中:
List<Object> list = new ArrayList<>();
但缺点是当你从列表中获得一个元素时,元素的静态类型将是Object
,你需要输入它所需的类型。
另请注意,您无法将int
或double
放入List
。原始类型不是引用类型,List
API要求元素是引用类型的实例。您需要使用相应的包装类型;即Integer
和Double
。
查看更多代码,我发现了这一点:
ArrayList<String> cityList =
new ArrayList<String>(Arrays.asList(cityLine.split(",")));
如果您将列表更改为对象为List<Object>
或Integer
的{{1}},则您无法像这样构建列表。
事实上,我看的越多,我就越认为你根本不需要一个清单。你应该做这样的事情:
Double
注意:根本没有 // 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]));
!!
答案 1 :(得分:0)
您可以在将字符串值传递到新的City
对象之前解析它们。然后你可以将City
中的构造函数和变量更改为int,double和double。
int pop = Integer.parseInt(cityList.get(3));
double latitude = Double.parseDouble(cityList.get(4));
double longitude = Double.parseDouble(cityList.get(5));
cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), pop, latitude, longitude);
答案 2 :(得分:0)
通过查看已定义的City
类,成员具有不同的原始数据类型。当您读取文件以创建City时,您需要使用构造函数中定义的数据类型传递构造函数参数。
修改您的City
课程,如下所示:
public class City {
String countrycode;
String city;
String region;
int population;
double latitude;
double longitude;
...
尝试以下方法:
cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), Integer.parseInt(cityList.get(3)), Double.parseDouble(cityList.get(4)), Double.parseDouble(cityList.get(5)));
这会将字符串转换为int,并按City
类的要求加倍。
答案 3 :(得分:0)
我相信不,但你可以这样做
public class City {
String countrycode;
String city;
String region;
String population;
double latitude;
double longitude;
public City (String countrycode, String city, String region, String population, double latitude, double 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;
}
}
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<String> cityList = new ArrayList<String>(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), Doule.parseDouble(cityList.get(4)), Doule.parseDouble(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();
}
}
答案 4 :(得分:0)
您可以这样做:
读者类
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
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<String> cityList = new ArrayList<String>(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), Integer.parseInt(cityList.get(3)), Double.parseDouble(cityList.get(4)), Double.parseDouble(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();
}
}
CITY CLASS
public class City {
String countrycode;
String city;
String region;
int population;
double latitude;
double longitude;
public City (String countrycode, String city, String region, int population, double latitude, double 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;
}
}
答案 5 :(得分:0)
有没有办法让它成为列表中的一些元素 不同类型的?
List
可能包含不同类型的元素,但如果您使用String.split()
填充它则不会 - 因为它会返回String[]
。
您可以使用Java基元类型包装器上的String.split()
方法将从[{1}}返回的字符串转换为所需类型,例如......
valueOf
答案 6 :(得分:0)
就像Stephen建议的那样,您可以使用List<Object>
,除此之外,您可以将String传递给City,但让City自己处理数据类型。
public class City {
String countrycode;
String city;
String region;
int population;
double latitude;
double longitude;
public City (String countrycode, String city, String region, String population, String latitude, String longitude) {
this.countrycode = countrycode;
this.city = city;
this.region = region;
try{
this.population = Integer.parseInt(population);
this.latitude = Double.parseDouble(latitude);
this.longitude = Double.parseDouble(longitude);
}catch(Exception e){
e.printStacktrace();
}
}
public String toString() {
return this.city + "," + this.population
+ "," + this.latitude + ","
+ this.longitude;
}
}
顺便说一句,您已经发起了In
两次。
In input = new In("world_cities.txt");
input = new In("world_cities.txt");
将其修改为
In input = new In("world_cities.txt");