将文本文件数据加载到 JTextfield

Loading text file data into JTextfield

为了完成这段代码,我已经搜索了两天各种教程,但是 none 实际上帮助我理解了我的代码有什么问题的概念。

我正在尝试写入文本文件,然后从中读入 Jtable。我正在使用数组。我的导师告诉我,我使用了太多列表 - 所以我尝试只使用一个 'constructorList',现在它无法编译。

出现的错误是:

error: method readFile in class CQUPestGUI cannot be applied to given types;

        readFile();
required: ArrayList<String>
found: no arguments
reason: actual and formal argument lists differ in length
1 error

我的任何纠正尝试似乎只会让事情变得更糟。

这部分代码似乎与错误有关。

 private void buttonLoadStoredContractsActionPerformed(java.awt.event.ActionEvent evt) {                                                          
        readFile();
}   

我跳过了一些部分,以免提供大量代码供通读

protected void readFile(ArrayList<String> listContractors)
{
   BufferedReader reader = null;
   //ArrayList showContract = new ArrayList();

   try
   {
       reader = new BufferedReader(new FileReader("Pest.txt"));
       String nLine = reader.readLine();


       while (nLine != null)
       {
           listContractors.add(nLine);
           String [] rows = nLine.split("-");
           for (String s: rows)
           {
                System.out.println(s);
           }
           nLine = reader.readLine();
       }
   }
   catch (FileNotFoundException e)
   {
       e.printStackTrace();
   }
   catch (IOException e)
   {
        e.printStackTrace();
   }
   displayStoredContracts(listContractors);
}

protected void displayStoredContracts(ArrayList<String> listContractors)
{
    for (int i = 0; i < listContractors.size(); i++)
    {
        txtAreaSavedContracts.append((String) listContractors.get(i));


    }
}

如果有人 想查看完整的代码,请在 pastebin 中查看它 -- full code。我使用 gui 生成器,因为我只是一个初学者并且有截止日期要满足 - 所以很多都会很混乱。我只是为了以防万一。

很容易找到,你的方法签名说,方法protected void readFile(ArrayList<String> listContractors)只接受String类型的java.util.ArrayList。但是当你调用方法时,你不传递任何参数,你通过readFile();调用方法。您应该传递一个 ArrayList 作为方法参数。

  1. 创建 ArrayList<String> list= new ArrayList<String>();
  2. 添加一些元素:list.add("asd");(根据需要)
  3. 调用方法:readFile(list);

希望对你有所帮助。

N.B。我认为 readFile 方法的定义不正确。它不应该将 ArrayList<String> 作为形式参数。它必须在方法块中创建此 ArrayList 的实例,并在方法边界内向其添加元素。