0

I'm trying to read the CSV file content into a java object. I found online resources that explains the two ways to read CSV ie., BufferReader/OpenCSV. But most of them are about reading row wise(I mean all the row data as one), the problem with my implementation is my CSV has the data in column wise like below:

UPDATE:

Name,A,B,C,D
JoinDate,1/1/2019,1/1/2018,06/01/2018,1/1/2019
Math_Marks,80,50,65,55
Social_Marks,80,50,86,95
Science_Marks,70,50,59,85
FirstLang_Marks,60,50,98,45
SecondLang_Marks,90,97,50

As you see the marks value are not mandatory, in above file person D has no marks listed for "SecondLang_Marks"

and my class object is below:

public class StudentVO {

private String name;
private Calendar joinDate;
private int math_Marks;
private int social_Marks;
private int science_Marks;
private int FirstLang_Marks;
private int secondLang_Marks;

// All get and set methods for class variables    
}

can anyone please help me to read the above csv vertically based on vertical headers and load the values into class object.

If possible can you please give both examples using BufferReader and OpenCSV.

Thanks

10
  • That is not a CSV file. You can probably use an HTML parser to parse that. Jsoup? Commented Jan 22, 2019 at 19:26
  • 1
    It is a HTML file, which only contains the Table tag data. CSV means Comma-separated values file which you will only read row wise, not column wise. Commented Jan 22, 2019 at 19:27
  • I'm sorry I'm just showing the format, the file looks something like above with delimiter as , Commented Jan 22, 2019 at 19:28
  • You need to show the actually file structure. Commented Jan 22, 2019 at 19:29
  • If a CSV file has columnar data you have to process the entire file with each column as a data object, as opposed to each row. Commented Jan 22, 2019 at 19:30

1 Answer 1

4

As far as i know, you can only read the data from file in row wise, there is not any mechanism to read the file vertically. But i have a solution for it, read the whole file, You can create a array of students and initialize it with default constructor and then set the data while reading downwards row wise.

try {
        BufferedReader reader = new BufferedReader(new FileReader("file.csv"));

        // Reading first line..
        String[] names = reader.readLine().split(",");
        // Execpt 'names' there are total 4 students, A,B,C,D.
        int totalStudents = names.length - 1;
        StudentVO[] array = new StudentVO[totalStudents];
        // Initialize all students with default constructor.
        for(int i = 0; i < array.length; i++) {
            array[i] = new StudentVO();
        }

        //////////////
        // Start reading other data and setting up on objects..
        // Line 2..
        String[] joinDates = reader.readLine().split(",");
        // i = 0 gives us the string 'joinDates' which is in the first column.
        // so we have to skip it and start it from i = 1
        for(int i = 1; i < joinDates.length; i++) {
            // setting the objects data..
            array[i - 1].setJoinDate(joinDates[i]); 
        }

        // And keep on doing this until SecondLang_Marks..

        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

This is the best way to do it for this solution according to the me.

Sign up to request clarification or add additional context in comments.

9 Comments

Sorry the file I'm trying to read is excel. Trying to convert it into CSV due to my system(app) support read format.
but, please let me know if it's possible to read excel file vertically
You can convert the excel file into CSV, after converting you can process it in java easily. Let me explain you the idea to do the magic, after creating a CSV, don't use openCSV etc., simply write your own algorithm to read the whole file using BufferedReader class, readLine() method reads a single line from file like on first run it reads '<table>', keep on merging the whole file data into the single String using a delimeter "\n" at end of each line, add keep track of comma when it cames, add that full string in ArrayList<String> and initialize the String back to "" and start doing it again.
After reading the whole file, run a for loop to the ArrayList<String>, each String in a list represent the one item of your excel column, and simply parse it using JSoup to read data from the HTML. :) Don't forget to accept the answer if it helps you, and give your remarks after trying it.
I updated the answer, do check it out, and don't forget to accept it, if it really helps. Because it is the good way to do the reading vertically.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.