2

I have requirement where I need to fetch few values and then write all those to csv file. To achieve this, I am adding the values to Java Arraylist and writing to a csv file. But when I do this Its writing the records to CSV but with empty rows in between records. Not sure why this problem is coming.

1,abc,fname,lname,email,log


1,abc1,fname1,lname1,email1,log1
1,abc2,fname2,lname2,email2,log2


1,abc3,fname3,lname3,email3,log3
1,abc4,fname4,lname4,email4,log4

here is my code. Please let me know what's wrong with this code:

public StringBuilder fetchUsers() {
    ArrayList<String> users = null;
    StringBuilder sb1 = new StringBuilder();
    try {
        client = getMyClient();
        if (client != null) {
            UserList usersList = getUsers();
            if (usersList.totalCount != 0 && usersList.totalCount >= 1) {
                System.out.println("usersList.totalCount ----->"
                        + usersList.totalCount);

                for (KalturaUser user : usersList.objects) {
                    users = new ArrayList<String>();
                    if (user != null) {
                        String action = null;
                        String userFullName = null;
                        String modifiedName = null;
                        String firstName = user.firstName;
                        if (user.id != null) {
                            String cnum = getUserUniqueId(user.id);
                            String userRole = getUserRole(user.id);
                            if (cnum != null || userRole != null) {
                                action = validateCnum(cnum, userRole);
                                if (!action.equals("3")) {
                                    users.add(action);
                                    users.add(cnum);
                                    if (firstName != null
                                            && firstName.contains("@")) {
                                        user.email = firstName;
                                        userFullName = getUserNames(user.id);
                                        firstName = userFullName;
                                    }
                                    users.add(firstName);
                                    users.add(user.lastName);
                                    users.add(user.email);
                                    users.add(userRole);
                                }
                            }
                        }
                    }
                    allUsers.add(users);
                }
            }
        }
    } catch (MyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    writeToCSV();
    return sb1;
}


private void writeToCSV() {
    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(System
            .getProperty("line.separator"));
    try {
        fileWriter = new FileWriter("C:/users.csv");
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
        csvFilePrinter.printRecord(USER_COLUMNS);
        for (List<String> rowData : allUsers) {
            csvFilePrinter.printRecord(rowData);
        }
    } catch (Exception e) {
        logger.error(e.getStackTrace());
    } finally {
        try {
            fileWriter.flush();
            fileWriter.close();
            csvFilePrinter.close();
        } catch (IOException e) {
            logger.error("Error while flushing/closing fileWriter/csvPrinter!");
            }
    }
}

1 Answer 1

5

This:

 allUsers.add(users);

should be inside the if (user != null) and all the sub-ifs

Something like this:

if (user != null) {
...
    if (user.id != null) {

        ...
        if (cnum != null || userRole != null) {
            ....
            if (!action.equals("3")) {
                users = new ArrayList<String>();
                ....
                allUsers.add(users);
            }
        }
    }
}

In general, you should create the array and add it to the allUsers only if you have something to add

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

4 Comments

Yes @Roman this would be the issue. From the code I felt this issue only.
@Roman - I have applied the change you mentioned. But still same issue.
You have put it inside the if (!action.equals("3"))?
@Roman - Thank you so much. Now its working. I understood the concept too. Many thanks to Stack exchange too to learn lot of things while working.

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.