【发布时间】:2017-09-11 16:27:54
【问题描述】:
正如标题所说,我正在尝试从名为 Hill 的类调用下面的方法到我的公共类,我还想将返回列表存储在本地列表中。
public static List<Hill> readHills() {
String destination = "****";
List<Hill> hill = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(new File(destination)));
String fileContents = "";
int line = 0;
while ((fileContents = br.readLine()) != null) {
if (line == 0) {
line++;
continue;
} else {
String[] entries = fileContents.split(",");
Hill placeholder = new Hill(Integer.parseInt(entries[0]), entries[1], entries[2], Double.parseDouble(entries[4]), Double.parseDouble(entries[5]), Double.parseDouble(entries[6]));
hill.add(placeholder);
line++;
}
}
}
catch(FileNotFoundException fnfe) {
System.out.println("Unable to find file");
}
catch(IOException e) {
System.out.println("Error");
}
return hill;
}
这是我试图调用上述方法的方法
public static void exercise5b() {
Hill mylist = new Hill();
//List<Integer> mylist = Hill.readHills();
}
但是,在执行此操作时,我在 Hill() 上遇到错误;声明 Hill 不能在 Hill() 中应用。下面的代码还显示了我的 Hill 类包含的内容
class Hill {
public int number;
public String name;
public String county;
public double height;
public double latitude;
public double longitude;
Hill (int number, String name, String county, double height, double latitude, double longitude){
this.number = number;
this.name = name;
this.county = county;
this.height = height;
this.latitude = latitude;
this.longitude = longitude;
}
【问题讨论】:
-
您已覆盖默认构造函数,因此在构造对象时必须为“Hill”构造函数提供参数。或者,您可以创建一个没有任何参数的构造函数,然后它将是有效的。