Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have a program that reads in a
CSV
file and stores the first and last lines as a
JSONObject
. My current implementation is skipping over the last line and reading in a
null
value as the data object.
What's the correct way to create/save a CSV file as to eliminate any white space or null values?
TIME_STAMP,TOTAL,APPLES,ORANGES,PEARS,GRAPES,TANGERINES,PINEAPPLES,CARROTS,UNKNOWN
7/1/2015 4:00,19474,1736,275,8366,5352,3003,393,349,
String firstLine = "";
String lastLine = "";
int count = 0;
if(reader != null){
String aux = "";
String lastLineMinusOne = "";
while ((aux = reader.readLine()) != null) {
if(count == 0)firstLine = aux;
lastLineMinusOne = lastLine;
lastLine = aux;
count ++;
logger.info("Count = " + count);
String[] columns = firstLine.split(",");
String[] data = lastLine.split(",");
logger.info(firstLine);
logger.info(lastLine);
2015-09-28 13:41:42,370 [ajp-0.0.0.0-8009-2] INFO com.ChartData - Count = 3
2015-09-28 13:23:27,745 [ajp-0.0.0.0-8009-3] INFO com.ChartData - TIME_STAMP,TOTAL,APPLES,ORANGES,PEARS,GRAPES,TANGERINES,PINEAPPLES,CARROTS,UNKNOWN
2015-09-28 13:23:27,745 [ajp-0.0.0.0-8009-3] INFO com.ChartData -
Error
java.lang.ArrayIndexOutOfBoundsException: 10
at com.ChartData.getCSV(ChartData.java:75)
Line 75 -> jObject.put("val", data[i]);
while ((aux = reader.readLine()) != null) {
String auxTrimmed = aux.replaceAll("(?m)^[ \t]*\r?\n", "");
// more code
If you experience this problem always, just ignore last line:
String[] data = lastLineMinusOne.split(",");
logger.info(lastLineMinusOne);
Usually, using some good library is a best way, if you do it not for education. CSV looks simple, until you face text in quotes, escape characters, different line endings and so on.
In your case you can use apache commons.csv
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.0</version>
</dependency>
and short usage example, note withIgnoreEmptyLines(true):
final CSVFormat format = CSVFormat.DEFAULT
.withIgnoreEmptyLines(true)
.withDelimiter(',');
CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"), format);
Iterator<CSVRecord> iterator = parser.iterator();
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.