I'll include the codes to compare two .csv files and have to display only the name of the student who has got odd marks in both files (not to display who got same marks in both files)
Main code
package comparecsv;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Comparecsv {
@SuppressWarnings("empty-statement")
public static void main(String[] args) throws FileNotFoundException, IOException {
//Input file which needs to be parsed
String fileToParse1 = "E://Java Project/SampleCSVFile1.csv";
String fileToParse2 = "E://Java Project/SampleCSVFile2.csv";
Comparecsv obj = new Comparecsv();
List<Student> studentList1 = obj.comparemarks(fileToParse1);
List<Student> studentList2 = obj.comparemarks(fileToParse2);
for(int i=0;i<studentList1.size();i++) {
Student s1 = studentList1.get(i);
for(int j=0;j<studentList2.size();j++) {
Student s2 = studentList2.get(j);
if(s1.getName().equals(s2.getName())) {
if(!s1.getAtt().equals(s2.getAtt())) {
System.out.println(s1.getName());
}
break;
}
}
}
}
public List<Student> comparemarks(String filename) {
BufferedReader fileReader = null;
List<Student> list = new ArrayList<Student>();
int i = 0;
//Delimiter used in CSV file
final String DELIMITER = ",";
try {
String line = "";
String name = null;
int atmark = 0;
//Create the file reader
fileReader = new BufferedReader(new FileReader(filename));
//Read the file line by line
Integer lineNo = 0;
while ((line = fileReader.readLine()) != null) {
//Get all tokens available in line
if(lineNo > 0) {
String[] tokens = line.split(DELIMITER);
Student s = new Student(Integer.parseInt(tokens[0]),tokens[1],tokens[2],Integer.parseInt(tokens[3]));
list.add(s);
}
lineNo++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
Below is the code for student class
package comparecsv;
public class Student {
Integer sno;
String name;
String dept;
Integer att;
public Student(Integer sno,String name,String dept,Integer att) {
this.sno = sno;
this.name = name;
this.dept = dept;
this.att = att;
}
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public Integer getAtt() {
return att;
}
public void setAtt(Integer att) {
this.att = att;
}
}
Now Is there possibilities to shorten the student class code? I'm a newbie, learning from friends. kindly help me guys.
the data in .csv files are
file 1
s.no,name,department,marks
1,ashok,cse,100
2,balu,cse,98
3,chandru,cse,99
file 2
s.no,name,department,marks
1,ashok,cse,99
2,balu,cse,100
3,chandru,cse,99
the output is
ashok
balu