Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为此添加return语句,以及如何调用该方法?_Java_Arraylist - Fatal编程技术网

Java 如何为此添加return语句,以及如何调用该方法?

Java 如何为此添加return语句,以及如何调用该方法?,java,arraylist,Java,Arraylist,readProductDataFile用于读取文本文件,并将其存储在Product[]类型的数组中。提供的代码无法更改,我需要一种处理此代码的方法。我已经设法使文件读取和排序到数组列表在另一个类中工作,但以这种方式运行它会给我带来一些问题: 1) 我无法从Main类调用readProductDataFile方法,好像它找不到该方法(它肯定在正确的包中) 2) 我不知道如何格式化return语句,我尝试了很多不同的方法,但我就是不知道如何将它存储为数组类型Product[] 到目前为止,我还没有提

readProductDataFile
用于读取文本文件,并将其存储在
Product[]
类型的数组中。提供的代码无法更改,我需要一种处理此代码的方法。我已经设法使文件读取和排序到数组列表在另一个类中工作,但以这种方式运行它会给我带来一些问题:

1) 我无法从
Main
类调用
readProductDataFile
方法,好像它找不到该方法(它肯定在正确的包中)

2) 我不知道如何格式化return语句,我尝试了很多不同的方法,但我就是不知道如何将它存储为数组类型
Product[]

到目前为止,我还没有提供很多具体的代码,因为我不想把答案放在盘子里交给我(这是作业的一部分,所以我不想让其他人直接为我做这件事),但有人能告诉我解决这个问题的正确方向吗

为了了解我目前的工作情况,以下测试代码对我很有用:

ElectronicsEquipmentDemo
class:

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)
}
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 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)
}
public class ElectronicsEquipmentDemo {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        Name inputFile = new Name();
        inputFile.privateName();
    }
}
它从文本文件中读取,如果行以p开头,则拆分为数组并打印出指定的值(尽管我尝试添加return语句使其仅返回第一行,因此仍在那里挣扎)。

这应该可以做到:

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;
    }
}

编辑:修复了范围问题。

对于第一个问题,我唯一能注意到的是您的方法是静态的,所以请确保您正确调用了它。 另外,考虑< <代码>静态< /代码>是否真的是你想要/需要的。


对于第二个问题,
return list.toArray(新字符串[list.size()])
应该可以工作。

我认为,try-and-catch不起作用,因为变量只在列表中存在。我尝试添加一个临时变量,但它也不喜欢(虽然我可能做得不正确)。需要在try-catch之外声明变量。try块具有父块的子范围。我早该抓到的。我已经返回并更新了答案。它说变量尚未初始化,但我编写了字符串[]list=null;这修复了它,谢谢:)我似乎无法访问主类中的列表,尽管返回了它,System.out.println(inputFile)打印了electronicsequipmentdemo。Name@1db9742It无法识别try-catch之外的return语句中的列表,也无法识别try-catch中的数组或大小,您可以使用临时变量,并在“try”之前声明它。然后,在“try”中,向该临时值添加一个您想要返回的值。当然,您还需要在类中导入java.util.ArrayList;是一个错误,因为列表已定义。这是我陷入困境的主要原因,我能想到的每一个补丁似乎都会破坏其他东西。我希望这会有所帮助。只要在捕获中添加抛出,就可以在try中包含return语句。因此,在try开始时将String[]list设置为null,然后在循环中添加一个值,然后返回它。