我将如何为此添加 return 语句,以及如何调用该方法? [英] How would I add a return statement for this, and how do I call the method?

查看:20
本文介绍了我将如何为此添加 return 语句,以及如何调用该方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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[] 类型的数组中.提供的代码无法更改,我需要一种使用此代码的方法.我已经设法使文件读取和排序到数组列表中在不同的类中工作,但是以这种方式运行它给我带来了一些问题:

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) 我无法从 Main 类调用 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 语句,我尝试了很多不同的东西,但我就是不知道如何将它存储为数组类型 Product[].

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[].

到目前为止我还没有提供很多具体的代码,因为我不希望将答案放在盘子上交给我(这是作业的一部分,所以我不希望其他人直接做对我来说),但是有人能指出我解决这个问题的正确方向吗?

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?

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

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;

    }
}

修复了范围问题.

这篇关于我将如何为此添加 return 语句,以及如何调用该方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆