Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Meaning of code? & possibility for shortening?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Meaning of code? & possibility for shortening?

    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

    // your code goes here
    String[] tokens = line.split(DELIMITER);
                        Student s = new Student(Integer.parseInt(tokens[0]),tokens[1],tokens[2],Integer.parseInt(tokens[3]));
                        list.add(s);

    The output of program is

    ashok
    balu


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Meaning of code? & possibility for shortening?

    String fileToParse1 = "E://Java Project/SampleCSVFile1.csv";
    String fileToParse2 = "E://Java Project/SampleCSVFile2.csv";

    I've added those two .csv files to my project folder of netbeans. But I dont know how to specify it in my above code guys. Kindly provide me the code as soon as possible guys

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Meaning of code? & possibility for shortening?

    Now I have to shorten the code for Student class.
    Why? What's the objective? When is the code too long? When is it short enough?
    Kindly provide me the code as soon as possible guys
    That's not how it works here, but maybe you meant to ask for help with the code rather than for the actual code.

    I would structure your code differently:

    For each file:
    While the file has lines of data:
    Read a line of the file
    Parse the data read from the file
    Create a Student object from the parsed data
    Next line of data
    Next file

    Compare the resulting Student objects to obtain the desired results

    Output results

    The above structure should be broken into as many methods as possible which are written to be shared/reused throughout the program.

Similar Threads

  1. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM