【问题标题】:How would I add a return statement for this, and how do I call the method?我将如何为此添加返回语句,以及如何调用该方法?
【发布时间】: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 语句使其仅返回第一行,所以仍然在那里挣扎)。

【问题讨论】:

    标签: java arraylist


    【解决方案1】:

    应该这样做:

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

    编辑:修复范围问题。

    【讨论】:

    • 这不会与 try and catch 一起工作,我认为因为变量只保留在列表中。我尝试添加一个临时变量,但它也不喜欢那样(虽然我可能做错了)。
    • 需要在try catch之外声明变量。 try 块具有父块的子范围。我应该抓住它。我已经回去更新了答案。
    • 说变量还没有初始化,但是我写的是String[] list = null;修复了它,谢谢:) 尽管返回了它,但我似乎无法访问主类中的列表, System.out.println(inputFile) 打印出 electronicsequipmentdemo.Name@1db9742
    【解决方案2】:

    好吧,对于第一个问题,我唯一能注意到的是您的方法是static,所以请确保您正确调用它。 另外,请考虑static 是否真的是您想要/需要的。

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

    【讨论】:

    • 在try catch之外不识别return语句中的list,在try catch内部也不识别toArray或size。
    • 好吧,你可以使用一个临时变量,并在“try”之前声明它。然后,在“try”中,向该临时添加一个您想要返回的值。当然,你还需要在你的类中导入 java.util.ArrayList。
    • 如果我这样做,String[] list = str.split("/");是一个错误,因为列表已经定义。这是我被卡住的主要原因,我能想到的每一个修复似乎都会破坏其他东西。
    • 我希望这会有所帮助。只要在 catch 中添加 throw,就可以在 try 中包含 return 语句。所以,你在 try 开始时将 String[] list 设置为 null,然后在循环中添加一个值,然后返回它。
    猜你喜欢
    • 2015-03-07
    • 2013-09-20
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多