Implementing data validation
After successfully reading the market data files, you've noticed that some files might be empty or missing important columns. Before performing any complex analysis, you need to implement basic validation checks to ensure the data files are usable. This will help prevent errors later in your data processing pipeline.
The Files, Path, Paths, and List classes have been imported for you.
This exercise is part of the course
Importing Data in Java
Exercise instructions
- Read all lines from the file into a list.
- Check if the file is empty.
- Verify the header contains the expected column names:
DateandTicker.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
public class SimpleDataValidation {
public static void main(String[] args) {
try {
// Read all lines from the file
Path path = Paths.____("sample_market_data.csv");
List lines = Files.____(path);
// Check if the file is empty
System.out.println("File is empty: " + ____.____());
// Verify the header contains expected column names
if (!lines.isEmpty()) {
String firstLine = lines.get(0);
System.out.println("First line: " + firstLine);
System.out.println("Contains 'Date': " + firstLine.____("____"));
System.out.println("Contains 'Ticker': " + firstLine.contains("____"));
System.out.println("Total lines: " + lines.size());
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
}
}