【发布时间】:2015-03-17 00:43:56
【问题描述】:
public class InputFileData {
/**
* @param inputFile a file giving the data for an electronic
* equipment supplier’s product range
* @return an array of product details
* @throws IOException
*/
public static Product [] readProductDataFile(File inputFile) throws IOException {
// CODE GOES HERE (input data from a text file and sort into arraylists)
}
readProductDataFile 用于读取文本文件,并将其存储在Product[] 类型的数组中。提供的代码无法更改,我需要一种与此代码一起使用的方法。我已经设法让文件读取和排序到数组列表在不同的类中工作,但是以这种方式运行它给我带来了几个问题:
1) 我无法从Main 类中调用readProductDataFile 方法,就好像它找不到方法一样(它肯定在正确的包中)。
2) 我不知道如何格式化 return 语句,我尝试了很多不同的方法,但我只是看不出如何将它存储为数组类型 Product[]。
到目前为止,我还没有提供很多具体的代码,因为我不想把答案放在盘子里交给我(这是作业的一部分,所以我不希望其他人直接做对我来说),但有人能指出我解决这个问题的正确方向吗?
为了了解我目前的情况,以下测试代码对我有用:
ElectronicsEquipmentDemo类:
public class ElectronicsEquipmentDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Name inputFile = new Name();
inputFile.privateName();
}
}
Name类:
public class Name {
public String privateName() {
try {
FileReader fr = new FileReader("myOutput.txt");
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br.readLine()) != null) {
char firstLetter = str.charAt(0);
if (firstLetter == 'P') {
String[] list = str.split("/");
Arrays.toString(list);
String fullName = list[1] + " " + list[2] + " " + list[3] + "\n";
System.out.println(fullName);
}
}
br.close();
} catch (IOException e) {
System.out.println("File not found");
}
return null;
}
}
从文本文件中读取,如果该行以 P 开头,则拆分为数组并打印出指定的值(尽管我尝试添加 return 语句使其仅返回第一行,所以仍然在那里挣扎)。
【问题讨论】: