问题描述
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
是用来读取一个文本文件,并将其存储在类型的数组产品[]
。提供的code无法改变,我需要与此code工作的一种方式。我已经成功地使文件读取和排序为数组列表在不同的类工作,但以这种方式运行它给我几个问题:
readProductDataFile
is used to read a text file, and store it in an array of type Product[]
. The code provided cannot be changed, I need a way that works with this code. I've managed to make file reading and sorting into array lists work in a different class, but running it in this way is giving me a couple of problems:
1)我不能调用从主
类的 readProductDataFile 方法,因为如果不能找对方法(这绝对是在正确的包)。
1) I can't call the readProductDataFile
method from the Main
class, as if it can't find the method (it's definitely in the correct package).
2)我无法弄清楚如何格式化return语句,我已经尝试了很多不同的东西,但我看不出如何将它保存为数组类型产品[]
。
2) I can't figure out how to format the return statement, I've tried lots of different things but I just can't see how to store it as array type Product[]
.
我还没有提供具体的很多code到目前为止,因为我不想要的答案被交给我放在盘子里(这是任务的一部分,所以我不希望其他人做直上做到这一点对我来说),但会有人能够指出我在正确的方向来解决这个?
I haven't provided a lot of specific code so far because I don't want the answer to be handed to me on a platter (this is part of an assignment so I don't want other people to straight up do it for me), but would anyone be able to point me in the right direction to solve this?
要给出如何我现在在做一个想法,下面的测试code为我工作:
To give an idea of how I'm doing at the moment, the following test code worked for me:
ElectronicsEquipmentDemo
类:
public class ElectronicsEquipmentDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Name inputFile = new Name();
inputFile.privateName();
}
}
名称
类:
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语句使它只返回第一线,所以还在挣扎那里)。
Which reads from a text file and, if the line begins with P, splits into arrays and prints out specified values (although my attempt to add a return statement made it only return the first line, so still struggling there).
推荐答案
这应该这样做:
public class Name {
public String[] privateName(){
String[] list;
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'){
list = str.split("/");
//Arrays.toString(list);
String fullName = list[1] + " " + list[2] + " "+ list[3] + "\n";
System.out.println(fullName);
}
}
br.close();
} catch (IOException e) {
out.println("File not found");
}
return list;
}
}
编辑:固定的范围界定问题
fixed scoping issue.
这篇关于我将如何添加一个return语句对于这一点,我该如何调用该方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!