繁体   English   中英

无法将Object []转换为String []

[英]Object[] cannot be converted to String[]

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

我的.CSV文件具有以逗号分隔的值: hey,hi 如果我只是想替换“嘿”,那么我将从文本字段中收集输入并将“嘿”替换为“再见”。 输出: 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文件?

首先, 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()方法的返回类型是

  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作为文件名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM