java - How would I extract a specific row in a .CSV file? -
the goal of code ask user input, user specifies input , code search , find out row user input located.
once located, have code extract row (string array?) can edit part of it's contents.
my problem i'm not sure code/methods/classes use in order this.
i have code set right reads .csv file , outputs string array of type list. iterate through array , find out if user input specified located within file.
at moment, code says if found, unsure how extract specific row, , hoping guys me out.
here's code @ click of button:
try{ string strsearch = searchserialfield.gettext(); csvreader reader = new csvreader(new filereader("test.txt"), ','); list<string[]> myentries = reader.readall(); reader.close(); //write existing file //csvwriter writer = new csvwriter(new filewriter("test.txt"), ',',csvwriter.no_quote_character, "\r\n"); //iterate through array find row user input located on (string[] line : myentries) { arraylist<string> newline = new arraylist<string>(); (string word : line) { if (word.contains(strsearch)) { //need method can find out row item on system.out.println("found - item on row: ..."); //code extract row (string array?) can edit it's contents //i need write edited row it's original row }else { system.out.println("not found"); } } } }catch (ioexception e) { system.out.println(e); } i've tried best i'm stumped how i'm meant extract row, how can go doing this?
thanks help.
what need not nested loop, arrays.tostring method.
import java.io.filereader; import java.io.ioexception; import java.util.arrays; import java.util.list; import com.opencsv.csvreader; public class test { public static void main(string[] arg){ search(); } public static void search(){ try{ string strsearch = "banana"; csvreader reader = new csvreader(new filereader("d:\\textfile.txt"), ','); list<string[]> myentries = reader.readall(); system.out.println(myentries.size()); reader.close(); //write existing file //csvwriter writer = new csvwriter(new filewriter("test.txt"), ',',csvwriter.no_quote_character, "\r\n"); //iterate through array find row user input located on int i=1; (string[] line : myentries) { string textline = arrays.tostring(line).replaceall("\\[|\\]",""); system.out.println(textline); if (textline.contains(strsearch)) { //need method can find out row item on system.out.println("found - item on row: ...:"+i); //code extract row (string array?) can edit it's contents //i need write edited row it's original row }else { system.out.println("not found"); } i++; } }catch (ioexception e) { system.out.println(e); } } }
Comments
Post a Comment