java - Comparing one array element to multiple array elements -
i have 2 text files have data below: file a:
4,12-98-1,38.4611,apo,tue,jan,07,14:31:34,2014 5,12-111-1,0.9520,apo,tue,jan,07,11:56:21,2014, 6,12-176-1,8.0510,apo,tue,jan,07,11:58:10,2014, 7,12-214-1,165.2349,apo,tue,jan,07,14:33:19,2014 8,12-293-1,61.2558,apo,tue,jan,07,14:35:11,2014 9,12-805-1,2.0987,apo,tue,jan,07,12:03:30,2014, 10,12-986-1,3.7740,apo,tue,jan,07,12:05:16,2014,
file b:
4,12-98-1,43.6546,apo,tue,jan,07,10:26:32,2014, 5,12-111-1,1.0430,apo,tue,jan,07,10:28:18,2014, 6,12-176-1,13.5606,apo,tue,jan,07,10:30:04,2014, 7,12-214-1,44.7091,apo,tue,jan,07,10:35:27,2014, 8,12-293-1,44.3001,apo,tue,jan,07,10:37:14,2014, 10,12-805-1,2.6451,apo,tue,jan,07,10:39:01,2014,
each of lines stored in array. want compare first array element of first line of file first array elements of lines in file b , first array element of second line of file first array elements of lines in file b , keep going.... till end of file.
i managed ones match while reading both text files. can`t match instance last line on each file first array element 10 on both files.
how can that?
here have done:
while ( (tkn24_line2 = tkn24br2.readline()) != null && (tkn2_line2 = tkn2br2.readline()) != null){ tkn24_array2 = tkn24_line2.split(","); int b = 0; for(string s : tkn24_array2){ //system.out.println("values[" + b + "] = " + s); b++; } tkn2_array2 = tkn2_line2.split(","); int c = 0; for(string s : tkn2_array2){ //system.out.println("values[" + c + "] = " + s); c++; } if(tkn24_array2[0].contentequals(tkn2_array2[0])){ //print something. } }
any appreciated!
if data doesn't fit in memory, have open , read file b every line of file a:
try (filereader fra = new filereader(filenamea); bufferedreader bra = new bufferedreader(fra)) { string linea; while ((linea = bra.readline()) != null) { string value = linea.split(",")[0]; try (filereader frb = new filereader(filenameb); bufferedreader brb = new bufferedreader(frb)) { boolean found = false; string lineb; while ((lineb = brb.readline()) != null) { string[] values = lineb.split(","); if (value.equals(values[0])) { if (!found) { system.out.println("matches " + value + ":"); } system.out.println(lineb); found = true; } } } catch (ioexception e) { e.printstacktrace(); } } } catch (ioexception e) { e.printstacktrace(); }
if data fit in memory, read lines 2 lists, , process lists using nested loops, or put lines of file b multimap indexed value @ beginning of line, , matching lines directly.
listmultimap<string, string> fileb = arraylistmultimap.create(); try (filereader frb = new filereader(filenameb); bufferedreader brb = new bufferedreader(frb)) { string lineb; while ((lineb = brb.readline()) != null) { string[] values = lineb.split(","); fileb.put(values[0], lineb); // map first value full line } } catch (ioexception e) { e.printstacktrace(); } try (filereader fra = new filereader(filenamea); bufferedreader bra = new bufferedreader(fra)) { string linea; while ((linea = bra.readline()) != null) { string value = linea.split(",")[0]; if (fileb.containskey(value)) { system.out.println("matches " + value + ":"); (string line : fileb.get(value)) { system.out.println(line); } } } } catch (ioexception e) { e.printstacktrace(); }
Comments
Post a Comment