Object []无法转换为String []

时间:2016-11-10 05:07:30

标签: java arrays list csv opencsv

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

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

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

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

但是,我无法将其写回文件,因为我得到的Object []无法转换为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文件?

2 个答案:

答案 0 :(得分:0)

首先,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()]));
        }

答案 1 :(得分:0)

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

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 []与不是true的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作为文件名,只要你在那里存储逗号分隔值。