This is the code for comparing the data in two .csv files.
file 1
s.no,name,department,marks
1,rahul,cse,100
2,sri,cse,98
3,arun,cse,99
file 2
s.no,name,department,marks
1,rahul,cse,99
2,sri,cse,100
3,arun,cse,99
I have to compare the marks of each student and display the name who has odd marks in both subject and not to display who equal equal marks in both files.
code :
// your code goes here 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 code for Student class
// your code goes here 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 I have to shorten the code for Student class. Kindly provide me info if there is possibilities. Also i've to know the exact of the tokens part in program. Tell me the function of following code
The output of program is
ashok
balu