I am using OpenCSV to read data from a CSV file and am using some of the sample code from the homepage:
CSVReader reader = new CSVReader(new FileReader("stockInfo.csv"));
List myEntries = reader.readAll();
And i am now trying to loop through this list and print out each entry. But i cannot seem to figure out the code to perform this.
Could anyone explain to me how i am supposed to do this becuase i just cant seem to work it out.
In this approach, we convert string to a character array using String. toCharArray() method. Then iterate the character array using for loop or for-each loop.
forEach() Since Java 8, we can use the forEach() method to iterate over the elements of a list. This method is defined in the Iterable interface, and can accept Lambda expressions as a parameter.
In java 8 you can use List. forEach() method with lambda expression to iterate over a list.
Assuming you want to output each entry of each line to it's own line:
List<String[]> myEntries = reader.readAll();
for (String[] lineTokens : myEntries) {
for (String token : lineTokens) {
System.out.println(token);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With