search a column word in csv file and replace it by another value java -
i have 2 csv file, main , look. main file contains list of word this:
a,c,e,f b,d,o,f
and look.csv contain 2 rows this:
a,f,j 1,0,1
i want search each word of main.csv , find match in look.csv. if there match, replace word in main.csv value in row 2 look.csv
i write code
public class lookup { public static void main(string args[]) throws filenotfoundexception { string word; scanner main = new scanner(new file("c:\\users\\sa\\desktop\\hotel2\\all.csv")); main.usedelimiter(","); scanner look=new scanner (new file("c:\\users\\sa\\desktop\\comtedad.csv")); while (main.hasnext()) { word=main.next() ; while (look.hasnext()){ if (main.next().equals(look.next())) {//code replacing} } } main.close(); }}
how can access second row of look.scv when 2 entry matched?
a few things note before posting solution:
- there no easy way replace contents of file while reading it, you have create new 1 , output need there
- if want replace values, place use map
- your code implies look.csv file contains 2 lines
the following achieve looking for, again, output in different file (you can rename afterwards if wish so) :
public class main { public static void main(string[] args) throws exception { //1. create scanners through csv files scanner main = new scanner(new file("main.csv")); scanner = new scanner(new file("look.csv")); //2. create final csv file contain both replaced words , non-replaced words main.csv filewriter replaced = new filewriter(createreplacedcsv()); //3. store contents of look.csv in map map<string, string> lookcsvwords = storelookcsvinset(look); //4. store contents of file in stringbuilder, can use stringbuilder.deletecharat(int) // , .lastindexof(string) delete last comma stringbuilder builder = new stringbuilder(); //5. keep track of line on int line = 0; //6. iterate through main.csv , search replaceable words, , write them replaced.csv if found while (main.hasnext()) { //search first line replaceable chars string[] wordsinline = main.nextline().split(","); (string word : wordsinline) { if (lookcsvwords.containskey(word) && line == 0) { word = lookcsvwords.get(word); } builder.append(word).append(","); } line ++; builder.deletecharat(builder.lastindexof(",")); builder.append("\n"); } //7. write contents of stringbuilder file replaced.write(builder.tostring()); replaced.close(); } /* * creates replaced.csv file if doesn't exist */ private static file createreplacedcsv() throws exception { file replacedcsv = new file("replaced.csv"); replacedcsv.createnewfile(); return replacedcsv; } /* * store words within look.csv in map. * method assumes look.csv has 2 lines */ private static map<string, string> storelookcsvinset(scanner lookscanner) throws exception { map<string, string> lookcsvwordmappings = new hashmap<string, string>(); string[] line1values = lookscanner.nextline().split(","); string[] line2values = lookscanner.nextline().split(","); (int i=0; i<line1values.length; i++) { lookcsvwordmappings.put(line1values[i], line2values[i]); } return lookcsvwordmappings; }
}
Comments
Post a Comment