【问题标题】:Object[] cannot be converted to String[]Object[] 不能转换为 String[]
【发布时间】:2016-11-10 05:07:29
【问题描述】:

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

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

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

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

但是,我无法将它写回文件,因为我收到 Object[] cannot be convert 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 文件?

【问题讨论】:

  • 使用泛型修改 myEntries 变量,如下所示。 List myEntries = reader.readAll();

标签: java arrays list csv opencsv


【解决方案1】:

一开始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()]));
        }

【讨论】:

  • 此代码不起作用。它会擦除我的整个文件 - 即使指定了用户输入。
  • 你要关闭作者吗? writer.close();
  • 我现在是,它现在以语音标记输出用户输入。它还会擦除整个文件,只显示用户输入。
  • 我很高兴您尝试了解我提供的代码来回答您关于为什么Object[] cannot be converted to String[] 我没有努力调试您的代码甚至完全理解它的问题。例如 - 您使用的是哪个 CSVWriter?
  • 我正在使用 OpenCSV。我已将 writer.close 放在 for 循环之外,它不再覆盖整个文件。但它确实将每个单词放入语音标记中,然后将它们全部放在一行中。
【解决方案2】:

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

List myEntries = reader.readAll();

我假设你没有注意到方法 readAll() 的返回类型是

  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 作为文件名,只要您在其中存储逗号分隔的值。

【讨论】:

  • 您也可以使用 reader.readNext() 方法逐行读取文件
猜你喜欢
  • 2014-05-08
  • 2018-07-16
  • 1970-01-01
  • 2013-04-24
  • 2013-05-24
  • 2014-01-29
  • 2013-01-30
  • 1970-01-01
  • 2014-05-16
相关资源
最近更新 更多