Object[] 无法转换为 String[]

Object[] cannot be converted to String[]

我的代码的目标是用文本字段的用户输入替换我的 .CSV 文件中的特定文本值。

我的 .CSV 文件包含以逗号分隔的值:hey,hi。如果我只想替换 'hey',那么我会从文本字段中收集输入并将 'hey' 替换为 'bye'。输出:bye,hi.

在我的代码中,我相信我正在读取我的文件并将文件的内容写入一个列表,用逗号分隔。

然后我将遍历列表并将列表中的用户输入实例替换为另一个用户输入并将其写回文件。

但是,我无法将其写回文件,因为我收到 Object[] cannot be converted to String[] 错误。因此,我对如何替换文本文件中的用户输入实例感到困惑。

这是我的代码:

try{

        //Convert user input into strings
        String strSerial = editSerialField.getText();
        String strLocation = editLocationField.getText();

        //Read existing file
        CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
        List myEntries = reader.readAll();

        //Iterate through my array
        for (int i = 0; i < myEntries.size(); i++)
        {
            //If an entry matches the user input
            if (myEntries.get(i).equals(strSerial))
            {
                //Set the match to the user input from strLocation
                myEntries.set(i, strLocation);
                break;
            }
        }

        //Write to existing file
        CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',');

        //Error is here**********************
        //Write the new string with the replaced word OVER the same file
        writer.writeNext(myEntries.toArray(new String[myEntries.size()]));
        writer.close();

        }catch (IOException e)
        {
            System.out.println(e);
        }
}

如何修改我的代码以便将我的更改写入 .CSV 文件?

一开始writeNext会一次在线写,所以你需要循环。

其次考虑不使用原始 List 而使用泛型。

第三,随手写可能会更干净

最后,每一行将包含一个字符串数组

考虑此代码(未测试)

        CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
        List<String []> myEntries = reader.readAll();
        reader.close ();

        CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',');

        //Iterate through my array
        for (String [] line : myEntries)
        {
            ArrayList<String> newLine = new ArrayList <String>();
            for (String word : line) {
            {
                String newVal = word.replace(strSerial, strLocation);
                newLine.add (newVal);
            }
            writer.writeNext(newLine.toArray(new String[newLine.size()]));
        }

您的问题是/从这一行开始:

List myEntries = reader.readAll();

我假设您没有注意到方法 readAll() 的 return 类型是

  List<String[]> 

例如,如果您的测试文件如下所示:

hey, hi
hallo, hello
sth, sthelse

调用 readAll() 后,您的变量 myEntries 将是一个字符串数组列表;每个数组代表文件中的每一行,该行中的每个字符串作为数组的元素

myEntries :  [hey, hi]
             [hallo, hello]
             [sth, sthelse]

牢记这一点,下一期是

if (myEntries.get(i).equals(strSerial)) 

您尝试将 String[] 与不正确的 String 进行比较。 按如下方式尝试:

try{
        //Convert user input into strings
        String strSerial = editSerialField.getText();
        String strLocation = editLocationField.getText();

        CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
        // valid but not a good practice how you declare your variable before
        // List myEntries = reader.readAll();  // List of what??
        List<String[]> myEntries = reader.readAll();

        for (String[] row : myEntries){         // go through each array from your list representing a row in your file
            for (int i = 0; i < row.length; i++){       //go through each element of that array
                if (row[i].equalsIgnoreCase(strSerial)){
                    row[i] = strLocation;
                }
            }
        }

        //add the parameter CSVWriter.NO_QUOTE_CHARACTER to prevent opencsv from writing quotes to file
        CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',',CSVWriter.NO_QUOTE_CHARACTER);
        for (String[] row : myEntries){
            writer.writeNext(row);
        }        
        writer.close();
    }catch (IOException e){
        System.out.println(e);
    }

不是很重要,但我更喜欢 test.csv 而不是 test.txt 作为文件名,只要您在其中存储逗号分隔值即可。