2016-11-10 117 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