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

why exactly would you want to 'shorten' that? there's not really anything in it.
you would do wise as to add a few methods, for instance, overriding the equals method and maybe the toString method, of course depending on what you need to do with the class.

I'm not sure what dept and att are supposed to be, but, for instance, if dept is department, you could create a class Department, with in it a List of Student instances, then you no longer need that dept variable.

but I don't know enough of your project's set-up or analysis, so hard to say that.

That Student class is a very standard kind of code, you'll find millions like it in real life. Tools like NetBeans give you ways to write it faster, but the final code is the same. The only tiny question is whether you need all the set nethods - eg do you want to be able to just change the sNo at any time? (note also the naming convention).

You can shorten and simplify your loops at lines 24/36 by using the "enhanced for loop"

for (Student s1 : studentList1) { 
   etc

when using Google Guava, you could also replace:

List<Student> list = new ArrayList<Student>();

by:

List<Student> list = Lists.newArrayList();

which performs the mapping of your generic type for you, but since you only have one of these, it might be a bit overkill to specially implement Guava just for that.
it contains some other nice utilities when working with lists.

Current versions of Java will infer the type of the new from the variable's declaration, so you can write
List<Student> list = new ArrayList<>();

I'm using netbeans. How can i add path for those two files in default project folder. I've copied my .csv files in my project folder of netbeans. But i don't know how to define the path in fileToparse command for both files. Resulted in errors.

Kindly provide me the path code as quick as possible guys.

path to which two files?
you shouldn't put your input files inside your project in netbeans, just add a path:
File fileOne = new File("C:/<pathTo>/file.cvs");

It would be a good idea to implement the logic like this:

public class Student implements Comparable<Student> {
    public Student(String pathToCVSFile) {
        initFromCVS(pathToCVSFile);
    }

    private initFromCVS(String cvsFilePath) {
        // Your logic to read and initialize instance variables
    }

    // Your code as appeared before

    @Override public int compare(Student other) {
        // Comparison criteria
    }
}

Since we're not comparing files, we are comparing students whose actual data are stored in files...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.