Get startedGet started for free

Reading CSV files

You've joined DataCorp as a junior data analyst. Your first project involves consolidating the employee database, but the data comes from multiple systems using different CSV formats - some comma-separated, others tab-delimited. Being able to handle varying file formats is essential since real-world data rarely comes in a single standardized format.

The Table, CsvReadOptions, and related Tablesaw classes have been imported for you.

This exercise is part of the course

Importing Data in Java

View Course

Exercise instructions

  • Read in "employees.csv" using default options.
  • Read "employees_tab.csv" using a tab delimiter.
  • Print the structure of both tables.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

public class ReadCSVFiles {
    public static void main(String[] args) {
        try {
            // Read file using default options
            Table standardCsv = Table.____().____("employees.csv");
            
            // Read file using a tab delimiter
            Table tabDelimited = Table.read().csv(CsvReadOptions
                .____("employees_tab.csv")
                .____('\t')
                .build());
            
            // Print the structure of both tables
            System.out.println(____.____());
            System.out.println(____.____());
            
        } catch (Exception e) {
            System.err.println("Error reading CSV files: " + e.getMessage());
        }
    }
}
Edit and Run Code