从 CSV 文件中读取数组
Read an array from a CSV file
(编辑,更新代码)
我正在尝试完成这项作业,该作业从 CSV 文件中读取一个数组,并且 returns 是一个汽车数组列表。我通过了五项测试中的两项,但我认为我的问题是末尾的“return null”。我不确定我应该做什么 return。我试过浏览我们的讲义并问过教授,但他们都没有帮助。我不是要答案,我只是需要指出正确的方向
到目前为止,这是我的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Practice reading CSV files
*
* @author 250 Instructors
* @version Feb 2017
*
*/
public class CarsForSale {
/**
* Read an array from CSV file
*
* @param aFile
* - string pointing to file
* @return the array list of Cars; return null if error encountered
*/
public static ArrayList<Car> getCarsFromCSVFile(String aFile) {
ArrayList<Car> cars = new ArrayList<Car>();
File file = new File(aFile);
try {
Scanner scan = new Scanner(file);
while (scan.hasNext()) {
String car = scan.nextLine();
}
scan.close();
}
catch (FileNotFoundException e) {
System.out.println("Failed to open file " + aFile);
System.out.println(e);
return null;
}
return cars;
}
}
JavaDoc 评论说:
@return the array list of Cars; return null if error encountered
如果您在 catch 块中 return null
并且在方法末尾 cars
,您将满足这些要求。
try {
// read from file
}
catch (FileNotFoundException e) {
// handle exception
return null;
}
您可以从 catch 块 here.
中阅读更多关于 returning 的信息
主要问题在于您return正在做什么和在哪里。 JavaDoc 说:
@return the array list of Cars; return null if error encountered
任何错误都应该被您的 catch 块捕获,这意味着您希望在该 catch 块的末尾 return null(如果您曾经输入它)。
对于 return Cars 的 ArrayList,首先您需要确保使用从文件中获取的输入创建 Car 对象,然后将这些对象添加到 ArrayList(而不是像你目前有)。
如果一切顺利,您应该 return try 块末尾的 Car 对象的 ArrayList。
try {
// read from file, create Car objects and add the objects to an ArrayList
return Cars;
}
catch (FileNotFoundException e) {
// handle exception
return null;
}
(编辑,更新代码) 我正在尝试完成这项作业,该作业从 CSV 文件中读取一个数组,并且 returns 是一个汽车数组列表。我通过了五项测试中的两项,但我认为我的问题是末尾的“return null”。我不确定我应该做什么 return。我试过浏览我们的讲义并问过教授,但他们都没有帮助。我不是要答案,我只是需要指出正确的方向
到目前为止,这是我的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Practice reading CSV files
*
* @author 250 Instructors
* @version Feb 2017
*
*/
public class CarsForSale {
/**
* Read an array from CSV file
*
* @param aFile
* - string pointing to file
* @return the array list of Cars; return null if error encountered
*/
public static ArrayList<Car> getCarsFromCSVFile(String aFile) {
ArrayList<Car> cars = new ArrayList<Car>();
File file = new File(aFile);
try {
Scanner scan = new Scanner(file);
while (scan.hasNext()) {
String car = scan.nextLine();
}
scan.close();
}
catch (FileNotFoundException e) {
System.out.println("Failed to open file " + aFile);
System.out.println(e);
return null;
}
return cars;
}
}
JavaDoc 评论说:
@return the array list of Cars; return null if error encountered
如果您在 catch 块中 return null
并且在方法末尾 cars
,您将满足这些要求。
try {
// read from file
}
catch (FileNotFoundException e) {
// handle exception
return null;
}
您可以从 catch 块 here.
中阅读更多关于 returning 的信息主要问题在于您return正在做什么和在哪里。 JavaDoc 说:
@return the array list of Cars; return null if error encountered
任何错误都应该被您的 catch 块捕获,这意味着您希望在该 catch 块的末尾 return null(如果您曾经输入它)。
对于 return Cars 的 ArrayList,首先您需要确保使用从文件中获取的输入创建 Car 对象,然后将这些对象添加到 ArrayList(而不是像你目前有)。
如果一切顺利,您应该 return try 块末尾的 Car 对象的 ArrayList。
try {
// read from file, create Car objects and add the objects to an ArrayList
return Cars;
}
catch (FileNotFoundException e) {
// handle exception
return null;
}