2016-11-10 118 views
0

我的代码的目标是将我的.CSV文件中的某个文本值替换为用户输入的文本字段。对象[]无法转换为字符串[]

我的.CSV文件的值由逗号分隔:hey,hi。如果我只是想替换'嘿',那么我会从文本字段中收集输入,并用'再见'代替'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文件?

+0

使用泛型来修改myEntries变量,如下所示。 列表 myEntries = reader.readAll(); – dammina

回答

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()])); 
     } 
+0

此代码不起作用。它擦除了我的整个文件 - 即使指定了用户输入。 – juiceb0xk

+0

你正在关闭作家吗? 'writer.close();' –

+0

我现在正在输出语音标记中的用户输入。它还擦除整个文件并仅显示用户输入。 – juiceb0xk

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 []与一个字符串不会是真实的。如下 试试:

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

+0

你也可以使用reader.readNext()方法来读取你的文件逐行 – Eritrean