I need to code a program to compare the datas in .csv file (.csv file is an excel file which will be seperated by , instead of cells by saving in.csv format). I've coded it fully and the required outout is also obtained. But i need a modified version of this code (logic can be changed or the entire functions can be changed but the outout is to be same)

package scannerexample;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ScannerExample

    @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";
        ScannerExample obj = new ScannerExample ();
        obj.highestAttenMark(fileToParse1);
        obj.highestAttenMark(fileToParse2);
    }

    public void highestAttenMark(String filename) {
        BufferedReader fileReader = null;
         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
            while ((line = fileReader.readLine()) != null) 
            {
                //Get all tokens available in line
                String[] tokens = line.split(DELIMITER);
              if(i!=0) {
                    if(i==0) {
                            name=tokens[1];
                            atmark=Integer.parseInt(tokens[3]);
                          }
                    if(atmark<=Integer.parseInt(tokens[3]))
                    {
                        name=tokens[1];
                        atmark=Integer.parseInt(tokens[3]);
                    }
                }
                i++;
                }
            System.out.println(name);
              }
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally
        {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

It is done by array concept, is there any other concepts to do it other than array. If so kindly provide me the info.
The data in .csv file is as follows

s.no,name,department,marks
1,naveen,ece,98
2,rahul,ece,95
3,kavi,ece,96

Actually i coded it for comparing data in each file (not comparing two files)

Recommended Answers

All 3 Replies

What do you mean by "compare"? Are you looking for duplicates, trying to sort the rows, or what?

well it is i've to compare the marks of the students in a .csv file and i've to display only the name of the student who has highest mark

OK.
Each row represents one student, with various data ablout that student. So the obvious thing to do is to define a Student class with sNo, name, department, and marks as instance variables. You can create a Student instance from each row of the data, and put those in an array(or some kind of List). You can then sort the Students according to whatever criteria, or simply loop through them to find the one with the highest marks etc.

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.