Alright I am having my program read two text files (in csv format) and once it reads them, I am wanting it to do a check to see if two columns match, if they do I want it to write it to a new file.
I have coded it, but am having problems getting it to function properly. I have set up print statements throughout the code (taken them out in the code I am posting so you don't get thrown off) and fixed some small issues here and there but still can't quite seem to find why its not working.
public class BeanInfoMerge {
private String itemA = "";
private String itemB = "";
private String itemC = "";
private String dataA = "";
private String dataB = "";
private String dataC = "";
public BeanInfoMerge() throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader("C:\\file1.csv"));
ArrayList<String> list1 = new ArrayList<String>();
try {
boolean bHeadersDone = false;
while (reader.ready()) {
String headerInfo = reader.readLine();
if (!bHeadersDone) {
if (headerInfo.contains("Option 3")) {
bHeadersDone = true;
}
}
else {
String[] info = list1Info.split("," , 3);
itemA = info[0];
itemB = info[1];
itemC = info[2];
if(!list1.equals(0)) {
list1.add(itemA);
list1.add(itemB);
list1.add(itemC);
BufferedReader reader2 = new BufferedReader(new FileReader("C:\\file2.csv"));
ArrayList<String> list2 = new ArrayList<String>();
try {
boolean bHeadersDone2 = false;
while (reader2.ready()) {
String headerInfo2 = reader2.readLine();
if (!bHeadersDone2) {
if (headerInfo2.contains("Characteristic 3")) {
bHeadersDone2 = true;
}
}
else {
String[] values = hostInfo.split("," , 3);
dataA = values[0];
dataB = values[1];
dataC = values[2];
if (!itemC.contains("[n/a]")) {
list2.add(itemA);
list2.add(itemB);
if (info[0].equals(values[0])) {
com.csvreader.CsvWriter writer = new com.csvreader.CsvWriter("C:\\final.csv");
writer.writeRecord(new String[]{"Item 1, Item 2, Option 1, Option 2"});
itemA = info[0];
itemB = info[1];
itemC = info[2];
dataA = values[0];
dataB = values[1];
dataC = values[2];
writer.writeRecord(new String[]{ itemB, itemC, dataB, dataC });
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here is what is located in file1.csv
Option 1, Option 2, Option 3
book, chapters, pages
phone, plastic, wires
ipod, music, video
Here is what is located in file2.csv
Characteristic 1, Characteristic 2, Characteristic 3
book, ink, paper
keyboard, keys, lights
phone, numbers, buttons
ipod, songs, movies
My final.csv remains empty, but here is what I am wanting it to look like:
Option 1, Option 2, Option 3, Characteristic 1, Characteristic 2
book, chapters, pages, ink, paper
phone, plastic, wires, numbers, buttons
ipod, music, video, songs, movies
Also I have noticed that when i put a print statement it prints it out like its a loop.
For instance, if I print out list2:
[book, ink, paper]
[book, ink, paper, keyboard, keys, lights]
[book, ink, paper, keyboard, keys, lights, phone, numbers, buttons]
[book, ink, paper, keyboard, keys, lights, phone, numbers, buttons, ipod, songs, movies]
Where as it should look like:
[book, ink, paper]
[keyboard, keys, lights]
[phone, numbers, buttons]
[ipod, songs, movies]
Would greatly appreciate advice and help. Mainly as to why csv writer isn't writing anything at all to the final.csv